Skip to content

Commit

Permalink
logging?
Browse files Browse the repository at this point in the history
  • Loading branch information
tsjensen committed Dec 2, 2023
1 parent 15c72d4 commit 8b38a59
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/logging.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* boxes - Command line filter to draw/remove ASCII boxes around text
* Copyright (c) 1999-2023 Thomas Jensen and the boxes contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License, version 3, as published by the Free Software Foundation.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/

/*
* Simple makeshift log facility, mostly used for handling debug output.
*/

#include <stdarg.h>
#include "tools.h"
#include "logging.h"

log_level_t current_log_level = INFO;

char *log_level_name[] = {
"TRACE", "DEBUG", "INFO", "WARN", "ERROR"
};


void log(log_level_t log_level, const char *module, const char *format, ...)
{
char *s = (char *) malloc(512);
va_list va;
va_start(va, format);
vsprintf(s, format, va);
va_end(va);

switch(log_level) {
case TRACE:
case DBG:
if (log_level >= current_log_level) {
bx_fprintf(stderr, "%5s %s: %s\n", log_level_name[log_level], module, s);
}
break;

case INFO:
if (log_level >= current_log_level) {
bx_fprintf(stdout, s);
}
break;

case WARN:
case ERROR:
if (log_level >= current_log_level) {
bx_fprintf(stderr, "%5s: %s\n", log_level_name[log_level], s);
}
break;
}
}


/* vim: set cindent sw=4: */
61 changes: 61 additions & 0 deletions src/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* boxes - Command line filter to draw/remove ASCII boxes around text
* Copyright (c) 1999-2023 Thomas Jensen and the boxes contributors
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License, version 3, as published by the Free Software Foundation.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/

/*
* Simple makeshift log facility.
*/

#ifndef LOGGING_H
#define LOGGING_H


typedef enum _log_level_t {
/** the finest log level */
TRACE,

/** a debug message, should be called `DEBUG` */
DBG,

/** normal informational message */
INFO,

/** a warning message, which should not happen, but can be tolerated */
WARN,

/** an error message, normally this will also lead to abnormal program termination */
ERROR
} log_level_t;


/** currently active log level, determine whether a message gets printed */
extern log_level_t current_log_level;


/** log level names for printing */
extern char *log_level_name[];


/**
* Print a log message. Log messages of level INFO will be printed in `stdout`, others on `stderr`.
* @param log_level the log level
* @param module the module name as per `__FILE__`
* @param format format string for the mesage, followed by the placeholder arguments
*/
void log(log_level_t log_level, const char *module, const char *format, ...);


#endif

/* vim: set cindent sw=4: */

0 comments on commit 8b38a59

Please sign in to comment.