Add log file

Plus refactoring out the logging.
This commit is contained in:
scorpion-26 2023-02-21 22:20:02 +01:00
parent 0e4b877654
commit 16933c24ff
5 changed files with 67 additions and 8 deletions

View file

@ -10,6 +10,7 @@ gtk_layer_shell = dependency('gtk-layer-shell-0')
headers = [
'src/Common.h',
'src/Log.h',
'src/System.h',
'src/PulseAudio.h',
'src/Widget.h',
@ -37,6 +38,8 @@ if get_option('WithSys')
add_global_arguments('-DWITH_SYS', language: 'cpp')
endif
add_global_arguments('-DUSE_LOGFILE', language: 'cpp')
pulse = dependency('libpulse')
libgBar = library('gBar',
@ -48,7 +51,8 @@ libgBar = library('gBar',
'src/BluetoothDevices.cpp',
'src/Plugin.cpp',
'src/Config.cpp',
'src/CSS.cpp'
'src/CSS.cpp',
'src/Log.cpp'
],
dependencies: [gtk, gtk_layer_shell, pulse],
install: true)

View file

@ -3,14 +3,9 @@
#include <unistd.h>
#include <string>
#include "Log.h"
#define UNUSED [[maybe_unused]]
#define LOG(x) std::cout << x << '\n'
#define ASSERT(x, log) \
if (!(x)) \
{ \
LOG(log << "\n[Exiting due to assert failed]"); \
exit(-1); \
}
// Flag helper macros
#define BIT(x) (1 << (x))

29
src/Log.cpp Normal file
View file

@ -0,0 +1,29 @@
#include "Log.h"
#include "Common.h"
#include <fstream>
namespace Logging
{
static std::ofstream logFile;
void Init()
{
logFile = std::ofstream("/tmp/gBar.log");
if (!logFile.is_open())
{
LOG("Cannot open logfile(/tmp/gBar.log)");
}
}
void Log(const std::string& str)
{
if (logFile.is_open())
logFile << str << std::endl;
}
void Shutdown()
{
logFile.close();
}
}

28
src/Log.h Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <sstream>
#ifdef USE_LOGFILE
#define LOG(x) \
std::cout << x << '\n'; \
{ \
std::stringstream str; \
str << x; \
Logging::Log(str.str()); \
}
#else
#define LOG(x) std::cout << x << '\n'
#endif
#define ASSERT(x, log) \
if (!(x)) \
{ \
LOG(log << "\n[Exiting due to assert failed]"); \
exit(-1); \
}
namespace Logging
{
void Init();
void Log(const std::string& str);
void Shutdown();
}

View file

@ -562,6 +562,8 @@ namespace System
void Init()
{
Logging::Init();
Config::Load();
#ifdef WITH_NVIDIA
@ -594,5 +596,6 @@ namespace System
#ifdef WITH_BLUEZ
StopBTScan();
#endif
Logging::Shutdown();
}
}