Skip to content

Commit

Permalink
Improve logging facilities
Browse files Browse the repository at this point in the history
Signed-off-by: Tin Švagelj <tin.svagelj@live.com>
  • Loading branch information
Caellian committed May 4, 2024
1 parent cc5b621 commit 499d15f
Show file tree
Hide file tree
Showing 13 changed files with 333 additions and 75 deletions.
8 changes: 2 additions & 6 deletions cmake/ConkyPlatformChecks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,9 @@ if(BUILD_COLOUR_NAME_MAP)
mark_as_advanced(APP_GPERF)
endif(BUILD_COLOUR_NAME_MAP)

if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG true)
endif(CMAKE_BUILD_TYPE MATCHES "Debug")

# The version numbers are simply derived from the date and number of commits
# since start of month
if(DEBUG)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
execute_process(COMMAND ${APP_GIT} --git-dir=${CMAKE_CURRENT_SOURCE_DIR}/.git
log --since=${VERSION_MAJOR}-${VERSION_MINOR}-01
--pretty=oneline
Expand All @@ -694,4 +690,4 @@ if(DEBUG)
RESULT_VARIABLE RETVAL
OUTPUT_VARIABLE COMMIT_COUNT
OUTPUT_STRIP_TRAILING_WHITESPACE)
endif(DEBUG)
endif()
2 changes: 0 additions & 2 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#ifndef _conky_config_h_
#define _conky_config_h_

#cmakedefine DEBUG

#define SYSTEM_NAME "@CMAKE_SYSTEM_NAME@"
#define PACKAGE_NAME "@PROJECT_NAME@"
#define VERSION "@VERSION@"
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ execute_process(

set(conky_sources
${conky_sources}
logging.cc
c++wrap.cc
c++wrap.hh
colour-settings.cc
Expand Down
4 changes: 2 additions & 2 deletions src/ccurl_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
#include "logging.h"
#include "text_object.h"

#ifdef DEBUG
#ifndef NDEBUG
#include <assert.h>
#endif /* DEBUG */
#endif /* NDEBUG */

#include <curl/easy.h>

Expand Down
4 changes: 2 additions & 2 deletions src/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ double loadgraphval(struct text_object *obj) {
uint8_t cpu_percentage(struct text_object *obj) {
if (static_cast<unsigned int>(obj->data.i) > info.cpu_count) {
NORM_ERR("obj->data.i %i info.cpu_count %i", obj->data.i, info.cpu_count);
CRIT_ERR("attempting to use more CPUs than you have!");
USER_ERR("attempting to use more CPUs than you have!");
}
if (info.cpu_usage != nullptr) {
return round_to_positive_int(info.cpu_usage[obj->data.i] * 100.0);
Expand All @@ -376,7 +376,7 @@ uint8_t cpu_percentage(struct text_object *obj) {
double cpu_barval(struct text_object *obj) {
if (static_cast<unsigned int>(obj->data.i) > info.cpu_count) {
NORM_ERR("obj->data.i %i info.cpu_count %i", obj->data.i, info.cpu_count);
CRIT_ERR("attempting to use more CPUs than you have!");
USER_ERR("attempting to use more CPUs than you have!");
}
if (info.cpu_usage != nullptr) { return info.cpu_usage[obj->data.i]; }
return 0.;
Expand Down
12 changes: 5 additions & 7 deletions src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ const char builtin_config_magic[] = "==builtin==";
// #define SIGNAL_BLOCKING
#undef SIGNAL_BLOCKING

/* debugging level, used by logging.h */
int global_debug_level = 0;

/* disable inotify auto reload feature if desired */
static conky::simple_config_setting<bool> disable_auto_reload(
"disable_auto_reload", false, false);
Expand Down Expand Up @@ -1915,6 +1912,7 @@ void clean_up(void) {

conky::cleanup_config_settings(*state);
state.reset();
conky::log::terminate_logging();
}

void handle_terminate() {
Expand Down Expand Up @@ -2216,31 +2214,31 @@ void initialisation(int argc, char **argv) {
case 'u':
state->pushnumber(strtod(optarg, &conv_end));
if (*conv_end != 0) {
CRIT_ERR("'%s' is an invalid update interval", optarg);
USER_ERR("'%s' is an invalid update interval", optarg);
}
update_interval.lua_set(*state);
break;

case 'i':
state->pushinteger(strtol(optarg, &conv_end, 10));
if (*conv_end != 0) {
CRIT_ERR("'%s' is an invalid number of update times", optarg);
USER_ERR("'%s' is an invalid number of update times", optarg);
}
total_run_times.lua_set(*state);
break;
#ifdef BUILD_X11
case 'x':
state->pushinteger(strtol(optarg, &conv_end, 10));
if (*conv_end != 0) {
CRIT_ERR("'%s' is an invalid value for the X-position", optarg);
USER_ERR("'%s' is an invalid value for the X-position", optarg);
}
gap_x.lua_set(*state);
break;

case 'y':
state->pushinteger(strtol(optarg, &conv_end, 10));
if (*conv_end != 0) {
CRIT_ERR("'%s' is a wrong value for the Y-position", optarg);
USER_ERR("'%s' is a wrong value for the Y-position", optarg);
}
gap_y.lua_set(*state);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2034,7 +2034,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long line,
END {
auto *buf = static_cast<char *>(malloc(text_buffer_size.get(*state)));

NORM_ERR("unknown variable '$%s'", s);
LOG_WARNING("unknown variable '$%s'", s);
snprintf(buf, text_buffer_size.get(*state), "${%s}", s);
obj_be_plain_text(obj, buf);
free(buf);
Expand Down
9 changes: 4 additions & 5 deletions src/darwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@
#endif

/* debugging defines */
#define DEBUG_MODE
#undef NDEBUG

/* (E)nhanced printf */
#ifdef DEBUG_MODE
#ifndef NDEBUG
#include <cstdarg>
void eprintf(const char *fmt, ...) {
va_list args;
Expand Down Expand Up @@ -928,9 +928,8 @@ void get_cpu_count() {
/*
* Allocate ncpus+1 slots because cpu_usage[0] is overall usage.
*/
info.cpu_usage =
static_cast<float *>(malloc((info.cpu_count + 1) * sizeof(float)));
if (info.cpu_usage == nullptr) { CRIT_ERR("malloc"); }
info.cpu_usage = new float[info.cpu_count + 1];
if (info.cpu_usage == nullptr) { CRIT_ERR("unable to allocate cpu_usage for %d cores", info.cpu_count + 1); }
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/display-output.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
namespace conky {

inline void log_missing(const char *name, const char *flag) {
DBGP(
LOG_INFO(
"%s display output disabled. Enable by recompiling with '%s' "
"flag enabled.",
name, flag);
Expand Down Expand Up @@ -87,7 +87,9 @@ bool initialize_display_outputs() {
register_output<output_t::X11>(outputs);
register_output<output_t::WAYLAND>(outputs);

for (auto out : outputs) { NORM_ERR("FOUND: %s", out->name.c_str()); }
for (auto out : outputs) {
LOG_TRACE("%s output display found.", out->name.c_str());
}

// Sort display outputs by descending priority, to try graphical ones first.
sort(outputs.begin(), outputs.end(), &display_output_base::priority_compare);
Expand Down
4 changes: 0 additions & 4 deletions src/freebsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@
#define KELVTOC(x) ((x - 2732) / 10.0)
#define MAXSHOWDEVS 16

#if 0
#define FREEBSD_DEBUG
#endif

kvm_t *kd;
std::mutex kvm_proc_mutex;

Expand Down
54 changes: 54 additions & 0 deletions src/logging.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "logging.h"

#include <algorithm>

namespace conky::log {
static const char *DEFAULT_LOG_PATH = "/tmp/conky.log";

static level current_log_level = level::DEBUG;

bool is_enabled(level log_level) {
return static_cast<int>(current_log_level) >= static_cast<int>(log_level);
}
void set_log_level(level log_level) { current_log_level = log_level; }
void log_more() {
current_log_level = static_cast<level>(std::min(
static_cast<int>(current_log_level) + 1, static_cast<int>(level::TRACE)));
}
void log_less() {
current_log_level = static_cast<level>(std::max(
static_cast<int>(current_log_level) - 1, static_cast<int>(level::OFF)));
}

static std::array<FILE *, 2> LOG_STREAMS{stderr, nullptr};
void use_log_file(const char *path) {
if (LOG_STREAMS[1] != nullptr) { fclose(LOG_STREAMS[1]); }

if (path != nullptr) {
LOG_STREAMS[1] = fopen(path, "a+");
} else {
LOG_STREAMS[1] = fopen(DEFAULT_LOG_PATH, "a+");
}
}

void init_system_logging() {
#ifdef HAS_SYSLOG
openlog(PACKAGE_NAME, LOG_PID, LOG_USER);
#else
use_log_file();
#endif
}

FILE **_log_streams() { return LOG_STREAMS.data(); };

void terminate_logging() {
if (LOG_STREAMS[1] != nullptr) {
fclose(LOG_STREAMS[1]);
LOG_STREAMS[1] = nullptr;
}
#ifdef HAS_SYSLOG
closelog();
#endif
}

} // namespace conky::log
Loading

0 comments on commit 499d15f

Please sign in to comment.