diff --git a/meson.build b/meson.build index 48b92c4..655140b 100644 --- a/meson.build +++ b/meson.build @@ -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) diff --git a/src/Common.h b/src/Common.h index 8dee1e2..1aa3347 100644 --- a/src/Common.h +++ b/src/Common.h @@ -3,14 +3,9 @@ #include #include +#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)) diff --git a/src/Log.cpp b/src/Log.cpp new file mode 100644 index 0000000..63bd6df --- /dev/null +++ b/src/Log.cpp @@ -0,0 +1,29 @@ +#include "Log.h" +#include "Common.h" + +#include + +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(); + } +} diff --git a/src/Log.h b/src/Log.h new file mode 100644 index 0000000..79df5a3 --- /dev/null +++ b/src/Log.h @@ -0,0 +1,28 @@ +#pragma once +#include + +#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(); +} diff --git a/src/System.cpp b/src/System.cpp index ead0e64..5e0b7bb 100644 --- a/src/System.cpp +++ b/src/System.cpp @@ -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(); } }