Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve logging and termination #1899

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
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
27 changes: 19 additions & 8 deletions lua/libcairo_imlib2_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,22 @@
#include <Imlib2.h>
#include <cairo.h>

#include "logging.h"

void cairo_place_image(const char *file, cairo_t *cr, int x, int y,
int width, int height, double alpha) {
#include "config.h"

#ifdef BUILD_I18N
#include <libintl.h>
#else
#define gettext
#endif

// TODO: inject reference to conky logger
// Lua allows modifying .so loading, so for each loaded library check if it has
// some hardcoded set_logger function symbol, and call it to set per-library
// reference to the global logger.
#define NORM_ERR(Format, ...) fprintf(stderr, gettext(Format), ##__VA_ARGS__);

void cairo_place_image(const char *file, cairo_t *cr, int x, int y, int width,
int height, double alpha) {
int w, h, stride;
Imlib_Image alpha_image, image, premul;
cairo_surface_t *result;
Expand Down Expand Up @@ -83,12 +95,12 @@ void cairo_place_image(const char *file, cairo_t *cr, int x, int y,
/* and use the alpha channel of the source image */
imlib_image_copy_alpha_to_image(alpha_image, 0, 0);

stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, width);
stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);

/* now pass the result to cairo */
result = cairo_image_surface_create_for_data(
(unsigned char *)imlib_image_get_data_for_reading_only(), CAIRO_FORMAT_ARGB32,
width, height, stride);
(unsigned char *)imlib_image_get_data_for_reading_only(),
CAIRO_FORMAT_ARGB32, width, height, stride);

cairo_set_source_surface(cr, result, x, y);
cairo_paint_with_alpha(cr, alpha);
Expand All @@ -101,7 +113,6 @@ void cairo_place_image(const char *file, cairo_t *cr, int x, int y,
imlib_free_image();

cairo_surface_destroy(result);

}

void cairo_draw_image(const char *file, cairo_surface_t *cs, int x, int y,
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
2 changes: 1 addition & 1 deletion src/ccurl_thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "logging.h"
#include "text_object.h"

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

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
30 changes: 14 additions & 16 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 @@ -1917,6 +1914,11 @@ void clean_up(void) {
state.reset();
}

void handle_terminate() {
clean_up();
std::abort();
}

static void set_default_configurations() {
update_uname();
info.memmax = 0;
Expand Down Expand Up @@ -2016,12 +2018,7 @@ void load_config_file() {
l.pushstring(current_config.c_str());
l.call(1, 1);
#else
char *syntaxerr;
if (asprintf(&syntaxerr, _(SYNTAX_ERR_READ_CONF), e.what())) {
std::string syntaxerrobj(syntaxerr);
free(syntaxerr);
throw conky::error(syntaxerrobj);
}
throw conky::error(SYNTAX_ERR_READ_CONF, e.what());
#endif
}
l.call(0, 0);
Expand All @@ -2030,7 +2027,7 @@ void load_config_file() {
l.getfield(-1, "text");
l.replace(-2);
if (l.type(-1) != lua::TSTRING) {
throw conky::error(_("missing text block in configuration"));
throw conky::error("missing text block in configuration");
}

/* Remove \\-\n. */
Expand Down Expand Up @@ -2087,7 +2084,7 @@ void set_current_config() {

/* : means that character before that takes an argument */
const char *getopt_string =
"vVqdDSs:t:u:i:hc:p:"
"vVqdDLSs:t:u:i:hc:p:"
#ifdef BUILD_X11
"x:y:w:a:X:m:f:"
#ifdef OWN_WINDOW
Expand All @@ -2103,7 +2100,8 @@ const char *getopt_string =
const struct option longopts[] = {
{"help", 0, nullptr, 'h'}, {"version", 0, nullptr, 'v'},
{"short-version", 0, nullptr, 'V'}, {"quiet", 0, nullptr, 'q'},
{"debug", 0, nullptr, 'D'}, {"config", 1, nullptr, 'c'},
{"debug", 0, nullptr, 'D'}, {"log-less", 0, nullptr, 'L'},
{"config", 1, nullptr, 'c'},
#ifdef BUILD_BUILTIN_CONFIG
{"print-config", 0, nullptr, 'C'},
#endif
Expand Down Expand Up @@ -2211,31 +2209,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
3 changes: 3 additions & 0 deletions src/conky.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ struct text_object;
/* sony support */
#include "sony.h"

void clean_up(void);
void handle_terminate();

/* A size for temporary, static buffers to use when
* one doesn't know what to choose. Defaults to 256. */
extern conky::range_config_setting<unsigned int> text_buffer_size;
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
10 changes: 3 additions & 7 deletions src/darwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@
#include <time.h>
#endif

/* debugging defines */
#define DEBUG_MODE

/* (E)nhanced printf */
#ifdef DEBUG_MODE
#if true // ifdef NDEBUG
#include <cstdarg>
void eprintf(const char *fmt, ...) {
va_list args;
Expand Down Expand Up @@ -928,9 +925,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
Loading