Commit 8232b1e3 authored by Christoffer Ackelman's avatar Christoffer Ackelman

Renamed co_debug to co_log, added optional logging to file and multiple logging levels.

parent 2e643250
...@@ -55,7 +55,7 @@ int main(int argc, char* argv[]) ...@@ -55,7 +55,7 @@ int main(int argc, char* argv[])
int sts; int sts;
char dev_name[20] = "eth1"; char dev_name[20] = "eth1";
setDebug(1); log_setLevel(LOG_TRACE);
QApplication app(argc, argv); QApplication app(argc, argv);
QApplication::setStyle(new PwrStyle()); QApplication::setStyle(new PwrStyle());
......
...@@ -228,15 +228,14 @@ static pwr_tStatus GetIoDeviceData(pwr_tAttrRef Object, const char* Attr, ...@@ -228,15 +228,14 @@ static pwr_tStatus GetIoDeviceData(pwr_tAttrRef Object, const char* Attr,
GsdmlDeviceData* data = new GsdmlDeviceData(); GsdmlDeviceData* data = new GsdmlDeviceData();
sts = data->read(datafile); sts = data->read(datafile);
if (EVEN(sts)) if (EVEN(sts)) {
{ delete data;
return sts; return sts;
} }
sts = data->get_value(Attr, Buf, BufSize); sts = data->get_value(Attr, Buf, BufSize);
delete data; delete data;
return sts; return sts;
} }
...@@ -251,19 +250,17 @@ static pwr_tStatus SetIoDeviceData(pwr_tAttrRef Object, const char* Attr, ...@@ -251,19 +250,17 @@ static pwr_tStatus SetIoDeviceData(pwr_tAttrRef Object, const char* Attr,
GsdmlDeviceData* data = new GsdmlDeviceData(); GsdmlDeviceData* data = new GsdmlDeviceData();
sts = data->read(datafile); sts = data->read(datafile);
if (EVEN(sts)) if (EVEN(sts)) {
{ delete data;
return sts; return sts;
} }
sts = data->modify_value(Attr, Value); sts = data->modify_value(Attr, Value);
if (ODD(sts)) if (ODD(sts)) {
{
data->print(datafile); data->print(datafile);
} }
delete data; delete data;
return sts; return sts;
} }
......
...@@ -58,7 +58,7 @@ int main(int argc, char* argv[]) ...@@ -58,7 +58,7 @@ int main(int argc, char* argv[])
unsigned int itemcnt; unsigned int itemcnt;
char servername[40] = ""; char servername[40] = "";
setDebug(1); log_setLevel(LOG_TRACE);
// Get options // Get options
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
......
...@@ -43,7 +43,7 @@ ...@@ -43,7 +43,7 @@
#include "co_cdh.h" #include "co_cdh.h"
#include "co_dcli.h" #include "co_dcli.h"
#include "co_dcli_msg.h" #include "co_dcli_msg.h"
#include "co_debug.h" #include "co_log.h"
#include "co_string.h" #include "co_string.h"
typedef enum { typedef enum {
...@@ -175,7 +175,7 @@ int dcli_replace_env(const char* str, char* newstr) ...@@ -175,7 +175,7 @@ int dcli_replace_env(const char* str, char* newstr)
if ((value = getenv(lower_symbol)) == NULL) { if ((value = getenv(lower_symbol)) == NULL) {
/* It was no symbol */ /* It was no symbol */
if (str_StartsWith(str, "$pwr")) { if (str_StartsWith(str, "$pwr")) {
debug_print("Warning! Could not resolve environment variable $%s\n", lower_symbol); log_debug("Warning! Could not resolve environment variable $%s\n", lower_symbol);
} }
*t = *s; *t = *s;
t++; t++;
...@@ -207,7 +207,7 @@ int dcli_replace_env(const char* str, char* newstr) ...@@ -207,7 +207,7 @@ int dcli_replace_env(const char* str, char* newstr)
if ((value = getenv(lower_symbol)) == NULL) { if ((value = getenv(lower_symbol)) == NULL) {
/* It was no symbol */ /* It was no symbol */
if (str_StartsWith(str, "$pwr")) { if (str_StartsWith(str, "$pwr")) {
debug_print("Warning! Could not resolve environment variable $%s\n", lower_symbol); log_debug("Warning! Could not resolve environment variable $%s\n", lower_symbol);
} }
*t = 0; *t = 0;
} else { } else {
......
...@@ -34,47 +34,84 @@ ...@@ -34,47 +34,84 @@
* General Public License plus this exception. * General Public License plus this exception.
*/ */
#include "co_debug.h" #include "co_log.h"
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
static int DEBUG = 0; static int LEVEL = 0;
static FILE* FP = NULL;
static int QUIET = 0;
void setDebug(int debug) void log_setLevel(int level)
{ {
DEBUG = debug; LEVEL = level;
} }
void log_setFile(FILE* fp)
{
FP = fp;
}
void log_setQuiet(int quiet)
{
QUIET = quiet;
}
static const char *level_names[] = {
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
};
static const char *level_colors[] = {
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
};
void print_time(FILE* stream, int fulldate) void print_time(FILE* stream, int fulldate)
{ {
time_t t;
struct tm* tm;
char Date[11], Time[11]; char Date[11], Time[11];
time(&t); time_t t = time(NULL);
tm = localtime(&t); struct tm* lt = localtime(&t);
if (fulldate) { if (fulldate) {
strftime(Date, 11, "%Y-%m-%d", tm); strftime(Date, 11, "%Y-%m-%d", lt);
fprintf(stream, "%s ", Date); fprintf(stream, "%s ", Date);
} }
strftime(Time, 11, "%H:%M:%S", tm); strftime(Time, 11, "%H:%M:%S", lt);
fprintf(stream, "%s", Time); fprintf(stream, "%s", Time);
} }
void dbg_print(const char* file, int line, const char* fmt, ...) void log_print(int level, const char* file, int line, const char* fmt, ...)
{ {
if (DEBUG) { if (level < LEVEL) {
return;
}
if (!QUIET) {
// 1. print timestamp // 1. print timestamp
print_time(stderr); print_time(stderr);
// 2. print filename only, without path // 2. print filename only, without path
const char* file2 = strrchr(file, '/'); const char* file2 = strrchr(file, '/');
file2 = file2 ? (file2 + 1) : file; file2 = file2 ? (file2 + 1) : file;
fprintf(stderr, " %s:%d: ", file2, line); fprintf(stderr, " %s%-5s\x1b[0m %s:%d: ",
level_colors[level], level_names[level], file2, line);
// 3. print the actual debug message // 3. print the actual debug message
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vfprintf(stderr, fmt, args); vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
} }
if (FP) {
// 1. print timestamp
print_time(FP);
// 2. print filename only, without path
const char* file2 = strrchr(file, '/');
file2 = file2 ? (file2 + 1) : file;
fprintf(FP, " %-5s %s:%d: ", level_names[level], file2, line);
// 3. print the actual debug message
va_list args;
va_start(args, fmt);
vfprintf(FP, fmt, args);
va_end(args);
}
} }
\ No newline at end of file
...@@ -36,16 +36,26 @@ ...@@ -36,16 +36,26 @@
// TODO: The functionality of this file overlaps that of rt_errh. // TODO: The functionality of this file overlaps that of rt_errh.
#ifndef CO_DEBUG #ifndef CO_LOG_H
#define CO_DEBUG #define CO_LOG_H
#include <stdarg.h>
#include <stdio.h> #include <stdio.h>
void setDebug(int debug); enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
void log_setLevel(int level);
void log_setQuiet(int quiet);
void log_setFile(FILE* fp);
void print_time(FILE* stream, int fulldate = 0); void print_time(FILE* stream, int fulldate = 0);
void dbg_print(const char* file, int line, const char* fmt, ...); void log_print(int level, const char* file, int line, const char* fmt, ...);
#define debug_print(fmt, args...) dbg_print(__FILE__, __LINE__, fmt, ##args); #define log_trace(...) log_print(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__);
#define log_debug(...) log_print(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__);
#define log_info(...) log_print(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__);
#define log_warn(...) log_print(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__);
#define log_error(...) log_print(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__);
#define log_fatal(...) log_print(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__);
#endif #endif
\ No newline at end of file
...@@ -39,8 +39,6 @@ ...@@ -39,8 +39,6 @@
#ifndef CO_UNIQUE_PTR #ifndef CO_UNIQUE_PTR
#define CO_UNIQUE_PTR #define CO_UNIQUE_PTR
#include "co_debug.h"
// std::unique_ptr for single objects -> free memory with 'delete' // std::unique_ptr for single objects -> free memory with 'delete'
template<typename T> template<typename T>
class unique_ptr { class unique_ptr {
......
...@@ -200,7 +200,7 @@ mkdir := mkdir ...@@ -200,7 +200,7 @@ mkdir := mkdir
# Set to /buildversion for frozen dbs versions # Set to /buildversion for frozen dbs versions
wblflags := wblflags :=
warnings := -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-parameter -Wno-unused-but-set-variable -Wno-sign-compare -Wno-missing-field-initializers -Wno-deprecated -Wno-cast-function-type warnings := -Wall -Wextra -Wno-unused-parameter -Wno-unused-but-set-parameter -Wno-unused-but-set-variable -Wno-sign-compare -Wno-missing-field-initializers -Wno-deprecated -Wno-cast-function-type -Wno-deprecated
ifeq ($(pwre_btype),rls) ifeq ($(pwre_btype),rls)
cflags := $(cross_compile) -c -O3 -D_GNU_SOURCE -DPWR_NDEBUG -D_REENTRANT -fPIC $(warnings) cflags := $(cross_compile) -c -O3 -D_GNU_SOURCE -DPWR_NDEBUG -D_REENTRANT -fPIC $(warnings)
......
...@@ -78,7 +78,7 @@ WVsel* WbQt::vsel_new(pwr_tStatus* status, const char* name, ...@@ -78,7 +78,7 @@ WVsel* WbQt::vsel_new(pwr_tStatus* status, const char* name,
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
setDebug(1); log_setLevel(LOG_TRACE);
if (argc >= 2 && streq(argv[1], "-m")) { if (argc >= 2 && streq(argv[1], "-m")) {
wb_erep::printMethods(); wb_erep::printMethods();
exit(0); exit(0);
...@@ -126,7 +126,7 @@ WbQt::WbQt(int argc, char* argv[]) ...@@ -126,7 +126,7 @@ WbQt::WbQt(int argc, char* argv[])
strcpy(volumename, "directory"); strcpy(volumename, "directory");
sw_projectvolume = 1; sw_projectvolume = 1;
arg_cnt = 0; arg_cnt = 0;
debug_print("%s ", argv[0]); log_debug("%s ", argv[0]);
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]); fprintf(stderr, "%s ", argv[i]);
if (argv[i][0] == '-') { if (argv[i][0] == '-') {
......
...@@ -40,7 +40,7 @@ ...@@ -40,7 +40,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "co_debug.h" #include "co_log.h"
#include "cow_style_qt.h" #include "cow_style_qt.h"
...@@ -79,7 +79,7 @@ CmdQt::CmdQt(int argc, char* argv[]) ...@@ -79,7 +79,7 @@ CmdQt::CmdQt(int argc, char* argv[])
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
setDebug(1); log_setLevel(LOG_TRACE);
QApplication app(argc, argv); QApplication app(argc, argv);
QApplication::setStyle(new PwrStyle()); QApplication::setStyle(new PwrStyle());
new CmdQt(argc, argv); new CmdQt(argc, argv);
......
...@@ -436,16 +436,16 @@ WAttText* WNavQt::watttext_new( ...@@ -436,16 +436,16 @@ WAttText* WNavQt::watttext_new(
void WNavQt::wge_subwindow_loop(WGe* wge) void WNavQt::wge_subwindow_loop(WGe* wge)
{ {
debug_print(" before wge_subwindow_loop\n"); log_debug(" before wge_subwindow_loop\n");
QApplication::exec(); QApplication::exec();
debug_print(" after wge_subwindow_loop\n"); log_debug(" after wge_subwindow_loop\n");
} }
void WNavQt::wge_modal_loop(WGe* wge) void WNavQt::wge_modal_loop(WGe* wge)
{ {
debug_print(" before wge_modal_loop\n"); log_debug(" before wge_modal_loop\n");
QApplication::exec(); QApplication::exec();
debug_print(" after wge_modal_loop\n"); log_debug(" after wge_modal_loop\n");
} }
void WNavQt::message_dialog(char* title, char* text) void WNavQt::message_dialog(char* title, char* text)
......
...@@ -56,7 +56,7 @@ void close_cb(void* ctx, void* xhelp) ...@@ -56,7 +56,7 @@ void close_cb(void* ctx, void* xhelp)
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
setDebug(1); log_setLevel(LOG_TRACE);
int sts; int sts;
QApplication app(argc, argv); QApplication app(argc, argv);
...@@ -80,7 +80,7 @@ CoHelpQt::CoHelpQt(int argc, char* argv[], int* return_sts) ...@@ -80,7 +80,7 @@ CoHelpQt::CoHelpQt(int argc, char* argv[], int* return_sts)
{ {
pwr_tStatus sts; pwr_tStatus sts;
debug_print("%s ", argv[0]); log_debug("%s ", argv[0]);
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]); fprintf(stderr, "%s ", argv[i]);
} }
......
...@@ -71,11 +71,11 @@ static void rtmon_close_cb(void* ctx) ...@@ -71,11 +71,11 @@ static void rtmon_close_cb(void* ctx)
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
setDebug(1); log_setLevel(LOG_TRACE);
int sts; int sts;
char display[80] = ""; char display[80] = "";
debug_print("%s ", argv[0]); log_debug("%s ", argv[0]);
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]); fprintf(stderr, "%s ", argv[i]);
if (streq(argv[i], "-h")) { if (streq(argv[i], "-h")) {
......
...@@ -71,13 +71,13 @@ static void statusmon_close(void* ctx) ...@@ -71,13 +71,13 @@ static void statusmon_close(void* ctx)
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
setDebug(1); log_setLevel(LOG_TRACE);
int sts; int sts;
int mode = nodelist_eMode_SystemStatus; int mode = nodelist_eMode_SystemStatus;
int view_descr = 0; int view_descr = 0;
char language[20] = ""; char language[20] = "";
debug_print("%s ", argv[0]); log_debug("%s ", argv[0]);
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]); fprintf(stderr, "%s ", argv[i]);
if (streq(argv[i], "-h")) { if (streq(argv[i], "-h")) {
......
...@@ -551,7 +551,7 @@ void XttQtWidget::valchanged_cmd_entry() ...@@ -551,7 +551,7 @@ void XttQtWidget::valchanged_cmd_entry()
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
setDebug(1); log_setLevel(LOG_TRACE);
int sts; int sts;
QApplication app(argc, argv); QApplication app(argc, argv);
...@@ -598,7 +598,7 @@ XttQt::XttQt(int argc, char* argv[], int* return_sts) ...@@ -598,7 +598,7 @@ XttQt::XttQt(int argc, char* argv[], int* return_sts)
strcat(title, nodename); strcat(title, nodename);
} }
debug_print("%s ", argv[0]); log_debug("%s ", argv[0]);
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]); fprintf(stderr, "%s ", argv[i]);
} }
......
...@@ -73,7 +73,7 @@ static int timeout_func() ...@@ -73,7 +73,7 @@ static int timeout_func()
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
setDebug(1); log_setLevel(LOG_TRACE);
pwr_tStatus sts; pwr_tStatus sts;
int i; int i;
char str[256]; char str[256];
...@@ -85,7 +85,7 @@ int main(int argc, char* argv[]) ...@@ -85,7 +85,7 @@ int main(int argc, char* argv[])
/* If arguments, treat them as a command and then exit */ /* If arguments, treat them as a command and then exit */
// Open directory volume as default // Open directory volume as default
debug_print("%s ", argv[0]); log_debug("%s ", argv[0]);
str[0] = 0; str[0] = 0;
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]); fprintf(stderr, "%s ", argv[i]);
......
...@@ -64,7 +64,7 @@ static void usage() ...@@ -64,7 +64,7 @@ static void usage()
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
setDebug(1); log_setLevel(LOG_TRACE);
int i; int i;
pwr_tFileName file; pwr_tFileName file;
char graph_name[80]; char graph_name[80];
...@@ -88,7 +88,7 @@ int main(int argc, char* argv[]) ...@@ -88,7 +88,7 @@ int main(int argc, char* argv[])
CoXHelpQt* xhelp = new CoXHelpQt(0, xhelp_eUtility_Wtt, &sts); CoXHelpQt* xhelp = new CoXHelpQt(0, xhelp_eUtility_Wtt, &sts);
CoXHelpQt::set_default(xhelp); CoXHelpQt::set_default(xhelp);
debug_print("%s ", argv[0]); log_debug("%s ", argv[0]);
if (argc > 1) { if (argc > 1) {
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]); fprintf(stderr, "%s ", argv[i]);
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
#ifndef QT_HELPERS_H #ifndef QT_HELPERS_H
#define QT_HELPERS_H #define QT_HELPERS_H
#include "co_debug.h" #include "co_log.h"
#include <QAction> #include <QAction>
#include <QComboBox> #include <QComboBox>
......
...@@ -434,7 +434,7 @@ void CoWowQtObject::DisplayLicense() ...@@ -434,7 +434,7 @@ void CoWowQtObject::DisplayLicense()
int CoWowQt::GetSelection(char* str, int size, const char* atom) int CoWowQt::GetSelection(char* str, int size, const char* atom)
{ {
debug_print("GetSelection str=%s, size=%d, arom=%s\n", str, size, atom); log_debug("GetSelection str=%s, size=%d, arom=%s\n", str, size, atom);
static wow_sSelection data; static wow_sSelection data;
data.received = 0; data.received = 0;
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment