diary

Text-based journaling program
git clone https://git.in0rdr.ch/diary.git
Log | Files | Refs | Pull requests |Archive | README | LICENSE

commit c30e550721fcaefcbc880b50caba469e23acf592
parent 0e52fa9549ad68a28ed933adec7ed3def5576464
Author: Andreas Gruhler <agruhl@gmx.ch>
Date:   Thu,  9 Jan 2025 00:19:21 +0100

feat: simple debug messsages to stderr

Diffstat:
MMakefile | 25++++++++-----------------
MREADME.md | 7+++----
Mdocs/TESTING.md | 91-------------------------------------------------------------------------------
Ddocs/trace.sh | 12------------
Mnix/diary-unstable.nix | 1-
Mnix/diary.nix | 1-
Mnix/shell.nix | 3---
Msrc/caldav.c | 80++++++++++++++++++++++++++++++++++++++++----------------------------------------
Dsrc/diary-tp.c | 5-----
Dsrc/diary-tp.h | 202-------------------------------------------------------------------------------
Msrc/diary.c | 14+++++++-------
Msrc/diary.h | 1-
Msrc/export.c | 4++--
Msrc/utils.c | 18+++++++++---------
Msrc/utils.h | 10++++++++--
15 files changed, 77 insertions(+), 397 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ TARGET = diary SRCDIR = src/ -_SRC = diary-tp.c export.c import.c utils.c caldav.c diary.c +_SRC = export.c import.c utils.c caldav.c diary.c SRC = $(addprefix $(SRCDIR), $(_SRC)) PREFIX ?= /usr/local BINDIR ?= $(DESTDIR)$(PREFIX)/bin @@ -15,40 +15,31 @@ UNAME = $(shell uname) XML_LIBS = $(shell pkg-config libxml-2.0 --libs) ifeq ($(UNAME),FreeBSD) - LIBS = -lncurses -lcurl $(ML_LIBS) -ldl -llttng-ust -pthread + LIBS = -lncurses -lcurl $(ML_LIBS) -ldl -pthread endif ifeq ($(UNAME),Linux) - LIBS = -lncursesw -lcurl $(XML_LIBS) -ldl -llttng-ust -pthread + LIBS = -lncursesw -lcurl $(XML_LIBS) -ldl -pthread endif ifeq ($(UNAME),Darwin) - LIBS = -lncurses -lcurl $(XML_LIBS) -ldl -llttng-ust -pthread -framework CoreFoundation + LIBS = -lncurses -lcurl $(XML_LIBS) -ldl -pthread -framework CoreFoundation endif default: $(TARGET) $(TARGET): $(SRC) - @# -I: Search this dir for header files (required for lttng) + @# -I: Search this dir for header files $(CC) -I $(SRCDIR) $(SRC) -o $(TARGET) $(CFLAGS) $(LIBS) -debug: $(SRC) lttng +debug: $(SRC) @# -g: Produce debugging information (for GDB) $(CC) -I $(SRCDIR) $(SRC) -o $(TARGET) $(CFLAGS) -g $(LIBS) -# The tracepoint provider package shared object can be -# preloaded before the instrumented application starts. -# https://lttng.org/docs/v2.13/#doc-building-tracepoint-providers-and-user-application -lttng: - @# -fpic: Generate position-independent code (PIC) suitable for shared library - $(CC) -I $(SRCDIR) -fpic -c $(SRCDIR)/diary-tp.c - @# create shared object file (.so) - $(CC) -shared -o libtpp.so diary-tp.o -llttng-ust -ldl - clean: - @# cleanup target and lttng shared object file - rm -f $(TARGET) libtpp.so diary-tp.o + @# cleanup target + rm -f $(TARGET) install: $(TARGET) install -m755 $(TARGET) $(BINDIR)/$(TARGET) diff --git a/README.md b/README.md @@ -75,10 +75,9 @@ Server = https://downloadcontent.opensuse.org/repositories/home:/in0rdr/Arch/$ar ``` ## Build from Source -1. The compilation step requires development libraries for `ncurses`, - `libcurl`, `libxml2` and `liblttng-ust`. These packages might have a - slightly different naming scheme depending on the distribution. To compile, - run: +1. The compilation step requires development libraries for `ncurses`, `libcurl` + and `libxml2`. These packages might have a slightly different naming scheme + depending on the distribution. To compile, run: ``` make diff --git a/docs/TESTING.md b/docs/TESTING.md @@ -30,98 +30,7 @@ To compile the program with clang: make CC=clang ``` -## Linux Trace Toolkit (LTTng) -> :information_source: For rapid debugging, use an LTTng live session, see -> [LTTng Live Tracing](#lttng-live-tracing). This section is especially useful -> for exporting the traces (in plain text) from a failed instance for support -> (e.g., attach to issue). - -Logs from the debug build can be recorded in a [LTTng tracing -session](https://lttng.org/docs/v2.13/#doc-start-sessiond). - -Start the daemon in the background: -``` -lttng-sessiond -b -``` - -To start a new session use: -```bash -lttng create -``` - -Create channel to capture the traces. The recording event rule below matches -all user space tracepoint events of which the name starts with `diary:*`: -```bash -lttng enable-event --userspace 'diary:*' -``` - -To only show traces at least as severe as `--loglevel` or for a specific log -level only: -```bash -lttng enable-event --userspace 'diary:*' --loglevel=WARNING -lttng enable-event --userspace 'diary:*' --loglevel-only=INFO -``` - -Build the debug diary with the [tracepoint provider package (tpp) shared -object](https://lttng.org/docs/v2.13/#doc-building-tracepoint-providers-and-user-application): -```bash -make debug -... -gcc -I src/ -fpic -c src/diary-tp.c -gcc -shared -o libtpp.so diary-tp.o -llttng-ust -ldl -``` - -Start recording session: -```bash -lttng start -``` - -Preload the tpp shared object and start diary: -```bash -LD_PRELOAD=./libtpp.so ./diary -``` - -Stop the session and observe traces: -```bash -lttng stop # stop session -lttng view # observe traces -lttng clear # if needed, clear traces -``` - -Cleanup the session daemon once done: -```bash -killall lttng-sessiond -``` - -An example (script) for viewing the traces is also given in -[./trace.sh](./trace.sh). - -## LTTng Live Tracing -[Live tracing](https://lttng.org/docs/v2.13/#doc-lttng-live) is more convenient -for rapid debugging. - -There is no need to stop the sessions to observe the logs. - -The setup is similar to the generic instructions above, but the session is -created with the `--live` flave. - -```bash -lttng-sessiond -b -lttng create --live -lttng enable-event --userspace 'diary:*' -lttng start -``` - -Then start the diary with the shared object file in a new terminal to view the -logs: -```bash -LD_PRELOAD=./libtpp.so ./diary -``` - ## Send stderr to File -> :information_source: For proper tracing and logging, start diary with the -> LTTng tracepoint provider package (tpp) shared object file, see -> [LTTng](#linux-trace-toolkit-lttng). Send stderr to a file for debugging: ```bash diff --git a/docs/trace.sh b/docs/trace.sh @@ -1,12 +0,0 @@ -#!/bin/bash -# -# This is an example that shows how to trace logs. -# source: https://github.com/lttng/lttng-ust/blob/master/doc/examples/cmake-multiple-shared-libraries/trace.sh - -lttng create -lttng enable-event --userspace 'diary:*' -lttng start -LD_PRELOAD=./libtpp.so ./diary -lttng stop -lttng view > trace.txt -cat trace.txt diff --git a/nix/diary-unstable.nix b/nix/diary-unstable.nix @@ -9,7 +9,6 @@ in ncurses curlFull libxml2 - lttng-ust ]; src = pkgs.fetchgit { diff --git a/nix/diary.nix b/nix/diary.nix @@ -9,7 +9,6 @@ in ncurses curlFull libxml2 - lttng-ust ]; src = pkgs.fetchgit { diff --git a/nix/shell.nix b/nix/shell.nix @@ -4,13 +4,10 @@ ncurses curlFull libxml2 - lttng-ust - lttng-tools valgrind scdoc groff mandoc pkg-config - #babeltrace2 ]; } diff --git a/src/caldav.c b/src/caldav.c @@ -38,7 +38,7 @@ static size_t curl_write_mem_callback(void* contents, size_t size, size_t nmemb, char* ptr = realloc(mem->memory, mem->size + realsize + 1); if (!ptr) { - tracepoint(diary, error, "Not enough memory (realloc in CURLOPT_WRITEFUNCTION returned NULL)"); + debug("%s", "Not enough memory (realloc in CURLOPT_WRITEFUNCTION returned NULL)"); return 0; } @@ -70,7 +70,7 @@ void get_access_token() { perror("Failure running oauth_eval_cmd in get_access_token()"); while (fgets(access_token, sizeof(char)*2048, fp) != NULL) - tracepoint(diary, debug_string, "New access token in get_access_token()", access_token); + debug("New access token in get_access_token(): %s", access_token); // strip any newline characters access_token[strcspn(access_token, "\n")] = '\0'; @@ -106,7 +106,7 @@ char* caldav_req(struct tm* date, char* url, char* http_method, const char* post curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, http_method); - tracepoint(diary, debug_string, "curl_easy_perform() request url", url); + debug("curl_easy_perform() request url: %s", url); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_mem_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&caldav_resp); @@ -158,8 +158,8 @@ char* caldav_req(struct tm* date, char* url, char* http_method, const char* post curl_easy_cleanup(curl); if (res != CURLE_OK) { - tracepoint(diary, error_string, "curl_easy_perform() failed in caldav_req()", curl_easy_strerror(res)); - tracepoint(diary, debug_long, "curl_easy_perform() response code", response_code); + debug("curl_easy_perform() failed in caldav_req(): %s", curl_easy_strerror(res)); + debug("curl_easy_perform() response code: %li", response_code); free(caldav_resp.memory); return NULL; } @@ -178,7 +178,7 @@ void put_event(struct tm* date, const char* dir, size_t dir_size, char* calendar fpath(dir, dir_size, date, &ppath, sizeof path); if (ppath == NULL) { - tracepoint(diary, error_date, "Cannot get file path for entry", date); + debug("Cannot get file path for entry %s", asctime(date)); return; } @@ -231,7 +231,7 @@ void put_event(struct tm* date, const char* dir, size_t dir_size, char* calendar uid, // todo: display first few chars of DESCRIPTION as SUMMARY folded_descr); - tracepoint(diary, debug_string, "PUT text/calendar", postfields); + debug("PUT text/calendar %s", postfields); strcat(calendar_uri, uid); strcat(calendar_uri, ".ics"); @@ -241,7 +241,7 @@ void put_event(struct tm* date, const char* dir, size_t dir_size, char* calendar free(descr); if (response == NULL) { - tracepoint(diary, error, "PUT request for event failed in put_event()"); + debug("%s", "PUT request for event failed in put_event()"); } // free memory allocated to store curl response @@ -265,15 +265,15 @@ int caldav_sync(struct tm* date, bool oauth_enabled = !(strcmp(CONFIG.oauth_eval_cmd, "") == 0); bool basicauth_enabled = !(strcmp(CONFIG.caldav_username, "") == 0 || strcmp(CONFIG.caldav_password, "") == 0); - tracepoint(diary, debug_int, "oauth_enabled: ", oauth_enabled); - tracepoint(diary, debug_int, "basicauth_enabled", basicauth_enabled); + debug("oauth_enabled: %i", oauth_enabled); + debug("basicauth_enabled: %i", basicauth_enabled); // check remote server and calendar are defined if (strcmp(CONFIG.caldav_server, "") == 0 || strcmp(CONFIG.caldav_calendar, "") == 0) { - tracepoint(diary, debug_string, "CONFIG.caldav_server", CONFIG.caldav_server); - tracepoint(diary, debug_string, "CONFIG.caldav_calendar", CONFIG.caldav_calendar); - tracepoint(diary, debug_string, "CONFIG.caldav_username", CONFIG.caldav_username); - tracepoint(diary, debug_string, "CONFIG.caldav_password", CONFIG.caldav_password); + debug("CONFIG.caldav_server: %s", CONFIG.caldav_server); + debug("CONFIG.caldav_calendar: %s", CONFIG.caldav_calendar); + debug("CONFIG.caldav_username: %s", CONFIG.caldav_username); + debug("CONFIG.caldav_password: %s", CONFIG.caldav_password); show_info(header, "CalDAV config incomplete, press any key to continue.", NULL); return -1; } @@ -281,9 +281,9 @@ int caldav_sync(struct tm* date, if (oauth_enabled) { if (strcmp(access_token, "") == 0) { // get acess token with oauth_eval_cmd - tracepoint(diary, debug_string, "Fetching access token using oauth_eval_cmd", CONFIG.oauth_eval_cmd); + debug("Fetching access token using oauth_eval_cmd %s", CONFIG.oauth_eval_cmd); get_access_token(); - tracepoint(diary, debug_string, "OAuth access token", access_token); + debug("OAuth access token: %s", access_token); } } @@ -291,11 +291,11 @@ int caldav_sync(struct tm* date, pthread_detach(progress_tid); char* user_principal = caldav_req(date, CONFIG.caldav_server, "PROPFIND", principal_request, 0, basicauth_enabled, "application/xml", 1); - tracepoint(diary, debug_string, "User principal return XML", user_principal); + debug("User principal return XML %s", user_principal); if (user_principal == NULL) { // The principal could not be fetched - tracepoint(diary, debug, "Unable to fetch user principal"); + debug("%s", "Unable to fetch user principal"); show_info(header, "Unable to fetch user principal, press any key to continue.", &progress_tid); return -1; } @@ -310,7 +310,7 @@ int caldav_sync(struct tm* date, free(user_principal); if (current_user_principal == NULL) { - tracepoint(diary, debug, "Unable to parse current user principal"); + debug("%s", "Unable to parse current user principal"); show_info(header, "Unable to parse current user principal, press any key to continue.", &progress_tid); return -1; } @@ -319,11 +319,11 @@ int caldav_sync(struct tm* date, char *caldav_host; char* caldav_host_scheme; CURLU *h = curl_url(); CURLUcode uc = curl_url_set(h, CURLUPART_URL, CONFIG.caldav_server, 0); uc = curl_url_get(h, CURLUPART_HOST, &caldav_host, 0); uc = curl_url_get(h, CURLUPART_SCHEME, &caldav_host_scheme, 0); - tracepoint(diary, debug_string, "Caldav server host name", caldav_host); - tracepoint(diary, debug_string, "Caldav server scheme/protocol", caldav_host_scheme); - tracepoint(diary, debug_string, "Caldav server host name", caldav_host); + debug("Caldav server host name: %s", caldav_host); + debug("Caldav server scheme/protocol: %s", caldav_host_scheme); + debug("Caldav server host name: %s", caldav_host); if (!uc) { - tracepoint(diary, error, "cur_url_get() failed in caldav_sync()"); + debug("%s", "cur_url_get() failed in caldav_sync()"); } curl_url_cleanup(h); @@ -332,7 +332,7 @@ int caldav_sync(struct tm* date, sprintf(uri, "%s://%s%s", caldav_host_scheme, caldav_host, current_user_principal); char* home_set = caldav_req(date, uri, "PROPFIND", homeset_request, 0, basicauth_enabled, "application/xml", 0); - tracepoint(diary, debug_string, "Home set xml", home_set); + debug("Home set xml %s", home_set); // parse home set from xml response char* home_set_parsed = extract_xml_content( @@ -342,7 +342,7 @@ int caldav_sync(struct tm* date, &progress_tid); if (home_set_parsed == NULL) { - tracepoint(diary, debug, "Unable to parse home set"); + debug("%s", "Unable to parse home set"); show_info(header, "Unable to parse home set, press any key to continue.", &progress_tid); return -1; } @@ -354,12 +354,12 @@ int caldav_sync(struct tm* date, // get calendar from home-set char* calendar_xml = caldav_req(date, uri, "PROPFIND", calendar_request, 1, basicauth_enabled, "application/xml", 0); - tracepoint(diary, debug_string, "Calendar set xml", calendar_xml); + debug("Calendar set xml: %s", calendar_xml); // get calendar URI from the home-set char calendar_xpath_query[300]; sprintf(calendar_xpath_query, "//*[local-name()='displayname'][text()='%s']/../../../*[local-name()='href']", CONFIG.caldav_calendar); - tracepoint(diary, debug_string, "Calendar xpath query", calendar_xpath_query); + debug("Calendar xpath query: %s", calendar_xpath_query); char* calendar_href = extract_xml_content( calendar_xml, calendar_xpath_query, @@ -367,7 +367,7 @@ int caldav_sync(struct tm* date, &progress_tid); if (calendar_href == NULL) { - tracepoint(diary, debug_string, "Could not find CalDAV calendar", CONFIG.caldav_calendar); + debug("Could not find CalDAV calendar %s", CONFIG.caldav_calendar); char* msg_fmtstr = "Could not find CalDAV calendar '%s', press any key to continue."; char msg[strlen(msg_fmtstr) + strlen(CONFIG.caldav_calendar)]; sprintf(msg, msg_fmtstr, CONFIG.caldav_calendar); @@ -395,7 +395,7 @@ int caldav_sync(struct tm* date, char* events_xml = caldav_req(date, uri, "REPORT", caldata_postfields, 1, basicauth_enabled, "application/xml", 0); if (events_xml == NULL) { - tracepoint(diary, debug, "Events could not be fetched"); + debug("%s", "Events could not be fetched"); show_info(header, "Events could not be fetched", &progress_tid); return -1; } @@ -411,7 +411,7 @@ int caldav_sync(struct tm* date, // no remote event found if (event == NULL) { - tracepoint(diary, debug, "No remote events found, continuing."); + debug("%s", "No remote events found, continuing."); event = ""; } @@ -441,8 +441,8 @@ int caldav_sync(struct tm* date, // check remote DTSTAMP of remote event char* remote_dtstamp = extract_ical_field(event, "DTSTAMP", &search_pos, false); char* remote_uid = extract_ical_field(event, "UID", &search_pos, false); - tracepoint(diary, debug_string, "event", event); - tracepoint(diary, debug_string, "DTSTAMP field", remote_dtstamp); + debug("Event: %s", event); + debug("DTSTAMP field: %s", remote_dtstamp); // init remote date to 1970, assume remote file does not exist remote_datetime = gmtime(&epoch_zero); @@ -455,17 +455,17 @@ int caldav_sync(struct tm* date, remote_date = mktime(remote_datetime); - tracepoint(diary, debug_string, "Remote last modified", ctime(&remote_date)); - tracepoint(diary, debug_string, "Local last modified", ctime(&localfile_date)); + debug("Remote last modified: %s", ctime(&remote_date)); + debug("Local last modified: %s", ctime(&localfile_date)); double timediff = difftime(localfile_date, remote_date); - tracepoint(diary, debug_double, "Time diff between local and remote mod time", timediff); + debug("Time diff between local and remote mod time: %f", timediff); char* rmt_desc; char dstr[16]; int conf_ch = 0; if (timediff == 0) { - tracepoint(diary, debug, "Local and remote files have equal timestamp or don't exist, giving up."); + debug("%s", "Local and remote files have equal timestamp or don't exist, giving up."); pthread_cancel(progress_tid); wclear(header); // free memory allocated to store curl response @@ -474,13 +474,13 @@ int caldav_sync(struct tm* date, free(remote_uid); return 0; } else if (timediff > 0) { - tracepoint(diary, debug, "Local file is newer. Uploading to remote."); + debug("%s", "Local file is newer. Uploading to remote."); if (remote_uid) { // purge any existing daily calendar entries on the remote side char event_uri[300]; sprintf(event_uri, "%s://%s%s%s.ics", caldav_host_scheme, caldav_host, calendar_href, remote_uid); //sprintf(event_uri, "%s%s%s.ics", CONFIG.caldav_server, calendar_href, remote_uid); - tracepoint(diary, debug_string, "Event URI for DELETE request", event_uri); + debug("Event URI for DELETE request: %s", event_uri); char* response = caldav_req(date, event_uri, "DELETE", NULL, 0, basicauth_enabled, "", 0); free(response); } @@ -494,7 +494,7 @@ int caldav_sync(struct tm* date, rmt_desc = extract_ical_field(event, "DESCRIPTION", &search_pos, true); if (rmt_desc == NULL) { - tracepoint(diary, error, "Could not fetch description of remote event. Aborting sync."); + debug("%s", "Could not fetch description of remote event. Aborting sync."); pthread_cancel(progress_tid); wclear(header); curl_free(caldav_host_scheme); @@ -517,7 +517,7 @@ int caldav_sync(struct tm* date, } if (conf_ch == 'y' || conf_ch == 'Y' || conf_ch == 'a' || conf_ch == '\n' || !confirm) { - tracepoint(diary, debug, "Remote file is newer. Extracting description from remote."); + debug("%s", "Remote file is newer. Extracting description from remote."); char* i; // persist downloaded buffer to local file diff --git a/src/diary-tp.c b/src/diary-tp.c @@ -1,5 +0,0 @@ -#define TRACEPOINT_CREATE_PROBES -#define TRACEPOINT_DEFINE -#define TRACEPOINT_PROBE_DYNAMIC_LINKAGE - -#include "diary-tp.h" diff --git a/src/diary-tp.h b/src/diary-tp.h @@ -1,202 +0,0 @@ -// LTTng 2.13 prepends the lttng_ust_ and LTTNG_UST_ prefix to all names -// to offer a consistent API namespace. Prefer the pre 2.13 behavior (API v0). -#define LTTNG_UST_COMPAT_API_VERSION 0 - -#undef TRACEPOINT_PROVIDER -#define TRACEPOINT_PROVIDER diary - -#undef TRACEPOINT_INCLUDE -#define TRACEPOINT_INCLUDE "diary-tp.h" - -#if !defined(_DIARY_TP_H) || defined(TRACEPOINT_HEADER_MULTI_READ) -#define _DIARY_TP_H - -#include <time.h> -#include <lttng/tracepoint.h> - -// Instrument a C user application -// https://lttng.org/docs/v2.13/#doc-c-application - -TRACEPOINT_EVENT( - diary, - error, - TP_ARGS( - char*, msg_arg - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ) -) - -TRACEPOINT_EVENT( - diary, - error_string, - TP_ARGS( - char*, msg_arg, - const char*, arg1 - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_string(arg1, arg1) - ) -) - -TRACEPOINT_EVENT( - diary, - error_date, - TP_ARGS( - char*, msg_arg, - const struct tm*, date_arg - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_string(date_arg, asctime(date_arg)) - ) -) - -TRACEPOINT_EVENT( - diary, - error_int_long, - TP_ARGS( - char*, msg_arg, - int, n_arg, - long, m_arg - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_integer(int, n, n_arg) - ctf_integer(long, m, m_arg) - ) -) - -TRACEPOINT_EVENT( - diary, - debug, - TP_ARGS( - char*, msg_arg - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ) -) - -TRACEPOINT_EVENT( - diary, - debug_string, - TP_ARGS( - char*, msg_arg, - char*, arg1 - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_string(arg1, arg1) - ) -) - -TRACEPOINT_EVENT( - diary, - debug_int, - TP_ARGS( - char*, msg_arg, - int, n_arg - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_integer(int, n, n_arg) - ) -) - -TRACEPOINT_EVENT( - diary, - debug_double, - TP_ARGS( - char*, msg_arg, - double, n_arg - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_integer(double, n, n_arg) - ) -) - -TRACEPOINT_EVENT( - diary, - debug_long, - TP_ARGS( - char*, msg_arg, - long, n_arg - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_integer(long, n, n_arg) - ) -) - -TRACEPOINT_EVENT( - diary, - debug_sizet, - TP_ARGS( - char*, msg_arg, - size_t, n_arg - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_integer(size_t, n, n_arg) - ) -) - -TRACEPOINT_EVENT( - diary, - debug_tm, - TP_ARGS( - char*, msg_arg, - const struct tm*, date - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_integer(int, tm_sec, date->tm_sec) - ctf_integer(int, tm_min, date->tm_min) - ctf_integer(int, tm_hour, date->tm_hour) - ctf_integer(int, tm_mday, date->tm_mday) - ctf_integer(int, tm_mon, date->tm_mon) - ctf_integer(int, tm_year, date->tm_year) - ctf_integer(int, tm_wday, date->tm_wday) - ctf_integer(int, tm_yday, date->tm_yday) - ctf_integer(int, tm_isdst, date->tm_isdst) - ) -) - -TRACEPOINT_EVENT( - diary, - warning_string, - TP_ARGS( - char*, msg_arg, - char*, arg1 - ), - TP_FIELDS( - ctf_string(msg, msg_arg) - ctf_string(arg1, arg1) - ) -) - -/* Log level assignment */ -// TRACE_ERR -// TRACE_WARNING -// TRACE_INFO -// TRACE_DEBUG_LINE (default) -TRACEPOINT_LOGLEVEL(diary, error, TRACE_ERR) -TRACEPOINT_LOGLEVEL(diary, error_string, TRACE_ERR) -TRACEPOINT_LOGLEVEL(diary, error_date, TRACE_ERR) -TRACEPOINT_LOGLEVEL(diary, error_int_long, TRACE_ERR) -TRACEPOINT_LOGLEVEL(diary, debug, TRACE_DEBUG) -TRACEPOINT_LOGLEVEL(diary, debug_string, TRACE_DEBUG) -TRACEPOINT_LOGLEVEL(diary, debug_int, TRACE_DEBUG) -TRACEPOINT_LOGLEVEL(diary, debug_double, TRACE_DEBUG) -TRACEPOINT_LOGLEVEL(diary, debug_long, TRACE_DEBUG) -TRACEPOINT_LOGLEVEL(diary, debug_sizet, TRACE_DEBUG) -TRACEPOINT_LOGLEVEL(diary, debug_tm, TRACE_DEBUG) -TRACEPOINT_LOGLEVEL(diary, warning_string, TRACE_WARNING) - -#endif /* _DIARY_TP_H */ - -#include <lttng/tracepoint-event.h> diff --git a/src/diary.c b/src/diary.c @@ -121,7 +121,7 @@ void display_entry(const char* dir, size_t dir_size, const struct tm* date, WIND // get entry path fpath(dir, dir_size, date, &ppath, sizeof path); if (ppath == NULL) { - tracepoint(diary, error_date, "Cannot get file path for entry", date); + debug("Cannot get file path for entry %s", asctime(date)); return; } @@ -239,7 +239,7 @@ void display_entry(const char* dir, size_t dir_size, const struct tm* date, WIND void edit_cmd(const char* dir, size_t dir_size, const struct tm* date, char** rcmd, size_t rcmd_size) { // set the edit command to env editor if (strlen(CONFIG.editor) + 2 > rcmd_size) { - tracepoint(diary, error, "Binary path of default editor too long"); + debug("%s", "Binary path of default editor too long"); *rcmd = NULL; return; } @@ -252,14 +252,14 @@ void edit_cmd(const char* dir, size_t dir_size, const struct tm* date, char** rc fpath(dir, dir_size, date, &ppath, sizeof path); if (ppath == NULL) { - tracepoint(diary, error_date, "Cannot get file path for entry", date); + debug("Cannot get file path for entry %s", asctime(date)); *rcmd = NULL; return; } // concatenate editor command with entry path if (strlen(*rcmd) + strlen(path) + 1 > rcmd_size) { - tracepoint(diary, error, "Edit command too long"); + debug("%s", "Edit command too long"); return; } strcat(*rcmd, path); @@ -306,7 +306,7 @@ bool read_config(const char* file_path) { // check if config file is readable if( access( config_file_path, R_OK ) != 0 ) { - tracepoint(diary, warning_string, "Config file missing or not readable, skipping", config_file_path); + debug("Config file missing or not readable, skipping. %s", config_file_path); return false; } @@ -769,7 +769,7 @@ int main(int argc, char** argv) { // get file path of entry and delete entry fpath(CONFIG.dir, diary_dir_size, &curs_date, &ppth, sizeof pth); if (ppth == NULL) { - tracepoint(diary, error_date, "Cannot get file path for entry", &curs_date); + debug("Cannot get file path for entry %s", asctime(&curs_date)); break; } @@ -806,7 +806,7 @@ int main(int argc, char** argv) { case 'e': case '\n': if (pecmd == NULL) { - tracepoint(diary, error, "Error retrieving edit command"); + debug("%s", "Error retrieving edit command"); break; } diff --git a/src/diary.h b/src/diary.h @@ -25,7 +25,6 @@ #include "import.h" #include "export.h" #include "caldav.h" -#include "diary-tp.h" #define XDG_CONFIG_HOME_FALLBACK "~/.config" #define CONFIG_FILE_PATH "diary/diary.cfg" diff --git a/src/export.c b/src/export.c @@ -59,7 +59,7 @@ void ics_export(const char* ics_filepath, WINDOW* header, WINDOW* cal, WINDOW* a fpath(CONFIG.dir, dirs, &it, &ppath, sizeof path); if (ppath == NULL) { - tracepoint(diary, error_date, "Cannot get file path for entry", &it); + debug("Cannot get file path for entry %s", asctime(&it)); return; } @@ -83,7 +83,7 @@ void ics_export(const char* ics_filepath, WINDOW* header, WINDOW* cal, WINDOW* a // read description bytes from journal entry file and append to descr int items_read = fread(descr + descr_label_size, sizeof(char), descr_bytes, fp); if (items_read != descr_bytes) { - tracepoint(diary, error_int_long, "Read n items but expected m items, aborting.", items_read, descr_bytes); + debug("Read %i items but expected %ld items, aborting.", items_read, descr_bytes); return; } diff --git a/src/utils.c b/src/utils.c @@ -20,7 +20,7 @@ bool date_has_entry(const char* dir, size_t dir_size, const struct tm* i) { fpath(dir, dir_size, i, &pepath, sizeof epath); if (pepath == NULL) { - tracepoint(diary, error_date, "Cannot get file path for entry", i); + debug("Cannot get file path for entry %s", asctime(i)); return false; } @@ -138,7 +138,7 @@ char* unfold(const char* str) { char* res = strtok(strcp, "\r\n"); if (res == NULL) { - tracepoint(diary, debug, "No more lines in multiline string, stop unfolding"); + debug("%s", "No more lines in multiline string, stop unfolding"); free(strcp); return NULL; } @@ -256,14 +256,14 @@ char* extract_xml_content(const char* xml, const char* xpathExpression, WINDOW* doc = xmlReadMemory(xml, strlen(xml), NULL, NULL, 0); if (doc == NULL) { - tracepoint(diary, debug, "Failed to parse xml"); + debug("%s", "Failed to parse xml"); show_info(w, "Failed to parse xml", p); return NULL; } xpathCtx = xmlXPathNewContext(doc); if (xpathCtx == NULL) { - tracepoint(diary, debug, "Failed to create xpath context"); + debug("%s", "Failed to create xpath context"); show_info(w, "Failed to create xpath context", p); xmlFreeDoc(doc); return NULL; @@ -272,7 +272,7 @@ char* extract_xml_content(const char* xml, const char* xpathExpression, WINDOW* xmlChar* xpath = (unsigned char *) xpathExpression; xpathObj = xmlXPathEvalExpression(xpath, xpathCtx); if(xpathObj == NULL) { - tracepoint(diary, debug, "Failed to evaluate xpath expression"); + debug("%s", "Failed to evaluate xpath expression"); show_info(w, "Failed to evaluate xpath expression", p); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); @@ -280,7 +280,7 @@ char* extract_xml_content(const char* xml, const char* xpathExpression, WINDOW* } char* nodeContent = (char *) xmlNodeGetContent(xpathObj->nodesetval->nodeTab[0]); - tracepoint(diary, debug_string, "XML node contents", nodeContent); + debug("XML node contents %s", nodeContent); xmlFreeDoc(doc); @@ -317,7 +317,7 @@ void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath { // check size of result path if (dir_size + 1 > rpath_size) { - tracepoint(diary, error, "Directory path too long"); + debug("%s", "Directory path too long"); *rpath = NULL; return; } @@ -329,7 +329,7 @@ void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath if (dir[dir_size - 1] != '/') { // check size again to accommodate '/' if (dir_size + 1 > rpath_size) { - tracepoint(diary, error, "Directory path too long"); + debug("%s", "Directory path too long"); *rpath = NULL; return; } @@ -341,7 +341,7 @@ void fpath(const char* dir, size_t dir_size, const struct tm* date, char** rpath // append date to the result path if (strlen(*rpath) + strlen(dstr) > rpath_size) { - tracepoint(diary, error, "File path too long"); + debug("%s", "File path too long"); *rpath = NULL; return; } diff --git a/src/utils.h b/src/utils.h @@ -15,12 +15,18 @@ #include <libxml/xpath.h> #include <libxml/tree.h> -#include "diary-tp.h" - #define CAL_WIDTH 21 #define ASIDE_WIDTH 4 #define MAX_MONTH_HEIGHT 6 +/* Create simple debug messsages to stderr + * https://stackoverflow.com/questions/1644868/define-macro-for-debug-printing-in-c + */ +#define debug(fmt, ...) \ + fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, \ + __LINE__, __func__, __VA_ARGS__); \ + fprintf(stderr, "\n"); + void update_date(WINDOW* header, struct tm* curs_date); bool date_has_entry(const char* dir, size_t dir_size, const struct tm* i); char* fold(const char* str);