Skip to content

Commit

Permalink
add logging of system and build information
Browse files Browse the repository at this point in the history
  • Loading branch information
d99kris committed Apr 20, 2024
1 parent afa326a commit 082cb35
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/common/src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

#pragma once

#define NCHAT_VERSION "4.62"
#define NCHAT_VERSION "4.63"
13 changes: 13 additions & 0 deletions lib/ncutil/src/strutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ std::string StrUtil::EscapeRawUrls(const std::string& p_Str)
return rv;
}

std::string StrUtil::ExtractString(const std::string& p_Str, const std::string& p_Prefix, const std::string& p_Suffix)
{
std::size_t prefixPos = p_Str.find(p_Prefix);
if (prefixPos != std::string::npos)
{
std::size_t suffixPos = p_Str.find(p_Suffix, prefixPos + p_Prefix.size());
std::size_t len = (suffixPos != std::string::npos) ? (suffixPos - prefixPos - p_Prefix.size()) : std::string::npos;
return p_Str.substr(prefixPos + p_Prefix.size(), len);
}

return "";
}

std::vector<std::string> StrUtil::ExtractUrlsFromStr(const std::string& p_Str)
{
std::string str = p_Str;
Expand Down
1 change: 1 addition & 0 deletions lib/ncutil/src/strutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class StrUtil
static void DeleteToPrevMatch(std::wstring& p_Str, int& p_Pos, int p_Offs, std::wstring p_Chars);
static std::string Emojize(const std::string& p_Str, bool p_Pad = false);
static std::string EscapeRawUrls(const std::string& p_Str);
static std::string ExtractString(const std::string& p_Str, const std::string& p_Prefix, const std::string& p_Suffix);
static std::vector<std::string> ExtractUrlsFromStr(const std::string& p_Str);
static std::string GetPass();
static std::string GetPhoneNumber();
Expand Down
80 changes: 80 additions & 0 deletions lib/ncutil/src/sysutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,83 @@
// nchat is distributed under the MIT license, see LICENSE for details.

#include "sysutil.h"

#include <sstream>

#include <unistd.h>

#include "fileutil.h"
#include "strutil.h"

std::string SysUtil::GetCompiler()
{
#if defined(__VERSION__)
#if !defined(__clang__) && defined(__GNUC__)
std::string compiler = "GCC " __VERSION__;
#else
std::string compiler = __VERSION__;
#endif
#else
std::string compiler = "Unknown compiler";
#endif

#if defined(__linux__)
#if defined(__GLIBC__) && defined(__GLIBC_MINOR__)
std::stringstream sslibc;
sslibc << "glibc " << __GLIBC__ << "." << __GLIBC_MINOR__;
std::string libc = sslibc.str();
#else
std::string libc = "non-glibc";
#endif
#else
std::string libc;
#endif

return compiler + (!libc.empty() ? " " + libc : "");
}

std::string SysUtil::GetOsArch()
{
static const std::string os = []()
{
#if defined(__linux__)
std::string str = FileUtil::ReadFile("/etc/os-release");
std::string prettyName = StrUtil::ExtractString(str, "PRETTY_NAME=\"", "\"");
return prettyName.empty() ? "Linux" : prettyName;
#elif defined(__APPLE__)
std::string str = FileUtil::ReadFile("/System/Library/CoreServices/SystemVersion.plist");
std::string name = StrUtil::ExtractString(str, "<key>ProductName</key>\n\t<string>", "</string>");
std::string ver = StrUtil::ExtractString(str, "<key>ProductVersion</key>\n\t<string>", "</string>");
return name + " " + ver;
#else
return "Unknown";
#endif
}();

static const std::string arch = []()
{
#if defined(__arm__)
return "arm";
#elif defined(__aarch64__)
return "arm64";
#elif defined(__x86_64__) || defined(__amd64__)
return "x86_64";
#elif defined(__i386__)
return "i386";
#else
return std::to_string(sizeof(void*) * 8) + "-bit";
#endif
}();

static const std::string osArch = os + " " + arch;
return osArch;
}

bool SysUtil::IsSupportedLibc()
{
#if defined(__APPLE__) || defined(__GLIBC__)
return true;
#else
return false;
#endif
}
6 changes: 6 additions & 0 deletions lib/ncutil/src/sysutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

#pragma once

#include <string>

#define UNUSED(x) SysUtil::Unused(x)

class SysUtil
{
public:
static std::string GetCompiler();
static std::string GetOsArch();
static bool IsSupportedLibc();

template<typename T>
static inline void Unused(const T& p_Arg)
{
Expand Down
16 changes: 10 additions & 6 deletions lib/wmchat/src/wmchat.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "config.h"
#include "protocol.h"
#include "sysutil.h"

class WmChat : public Protocol
{
Expand All @@ -25,12 +26,15 @@ class WmChat : public Protocol
static std::string GetCreateFunc() { return "CreateWmChat"; }
static std::string GetSetupMessage()
{
#if defined(__APPLE__) || defined(__GLIBC__)
return "";
#else
return "\nUNSUPPORTED PLATFORM:\nThe WhatsApp protocol implementation officially only supports glibc on Linux.\n"
"For details, refer to https://github.com/d99kris/nchat/issues/204\n";
#endif
if (SysUtil::IsSupportedLibc())
{
return "";
}
else
{
return "\nUNSUPPORTED PLATFORM:\nThe WhatsApp protocol implementation officially only supports glibc on Linux.\n"
"For details, refer to https://github.com/d99kris/nchat/issues/204\n";
}
}

std::string GetProfileId() const;
Expand Down
31 changes: 29 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "profiles.h"
#include "scopeddirlock.h"
#include "status.h"
#include "sysutil.h"
#include "ui.h"

#ifdef HAS_DUMMY
Expand All @@ -38,6 +39,7 @@
#include "wmchat.h"
#endif

static std::string GetFeatures();
static void RemoveProfile();
static std::shared_ptr<Protocol> SetupProfile();
static void ShowHelp();
Expand Down Expand Up @@ -244,7 +246,13 @@ int main(int argc, char* argv[])
const std::string& logPath = FileUtil::GetApplicationDir() + std::string("/log.txt");
Log::Init(logPath);
std::string appNameVersion = AppUtil::GetAppNameVersion();
LOG_INFO("starting %s", appNameVersion.c_str());
LOG_INFO("%s", appNameVersion.c_str());
std::string osArch = SysUtil::GetOsArch();
LOG_INFO("%s", osArch.c_str());
std::string compiler = SysUtil::GetCompiler();
LOG_INFO("%s", compiler.c_str());
std::string features = GetFeatures();
LOG_INFO("%s", features.c_str());

// Init signal handler
AppUtil::InitSignalHandler();
Expand Down Expand Up @@ -408,13 +416,32 @@ int main(int argc, char* argv[])
rv = 1;
}

LOG_INFO("exiting nchat");
LOG_INFO("exit");

Log::Cleanup();

return rv;
}

std::string GetFeatures()
{
std::string features =
#if defined(HAS_TELEGRAM)
"Telegram ON, "
#else
"Telegram OFF, "
#endif

#if defined(HAS_WHATSAPP)
"WhatsApp ON"
#else
"WhatsApp OFF"
#endif
;

return features;
}

void RemoveProfile()
{
// Show profiles
Expand Down
2 changes: 1 addition & 1 deletion src/nchat.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\" DO NOT MODIFY THIS FILE! It was generated by help2man.
.TH NCHAT "1" "April 2024" "nchat v4.62" "User Commands"
.TH NCHAT "1" "April 2024" "nchat v4.63" "User Commands"
.SH NAME
nchat \- ncurses chat
.SH SYNOPSIS
Expand Down
4 changes: 2 additions & 2 deletions src/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void Ui::Run()
MessageCache::FetchContacts(protocol.first);
}

LOG_INFO("entering ui loop");
LOG_INFO("ui loop start");

raw();
curs_set(1);
Expand All @@ -105,7 +105,7 @@ void Ui::Run()
}
}

LOG_INFO("exiting ui loop");
LOG_INFO("ui loop end");

// set as offline before logging off
for (auto& protocol : protocols)
Expand Down

0 comments on commit 082cb35

Please sign in to comment.