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[])
int sts;
char dev_name[20] = "eth1";
setDebug(1);
log_setLevel(LOG_TRACE);
QApplication app(argc, argv);
QApplication::setStyle(new PwrStyle());
......
......@@ -228,15 +228,14 @@ static pwr_tStatus GetIoDeviceData(pwr_tAttrRef Object, const char* Attr,
GsdmlDeviceData* data = new GsdmlDeviceData();
sts = data->read(datafile);
if (EVEN(sts))
{
if (EVEN(sts)) {
delete data;
return sts;
}
sts = data->get_value(Attr, Buf, BufSize);
delete data;
return sts;
}
......@@ -251,19 +250,17 @@ static pwr_tStatus SetIoDeviceData(pwr_tAttrRef Object, const char* Attr,
GsdmlDeviceData* data = new GsdmlDeviceData();
sts = data->read(datafile);
if (EVEN(sts))
{
if (EVEN(sts)) {
delete data;
return sts;
}
sts = data->modify_value(Attr, Value);
if (ODD(sts))
{
if (ODD(sts)) {
data->print(datafile);
}
delete data;
return sts;
}
......
......@@ -58,7 +58,7 @@ int main(int argc, char* argv[])
unsigned int itemcnt;
char servername[40] = "";
setDebug(1);
log_setLevel(LOG_TRACE);
// Get options
for (int i = 1; i < argc; i++) {
......
......@@ -43,7 +43,7 @@
#include "co_cdh.h"
#include "co_dcli.h"
#include "co_dcli_msg.h"
#include "co_debug.h"
#include "co_log.h"
#include "co_string.h"
typedef enum {
......@@ -175,7 +175,7 @@ int dcli_replace_env(const char* str, char* newstr)
if ((value = getenv(lower_symbol)) == NULL) {
/* It was no symbol */
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++;
......@@ -207,7 +207,7 @@ int dcli_replace_env(const char* str, char* newstr)
if ((value = getenv(lower_symbol)) == NULL) {
/* It was no symbol */
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;
} else {
......
......@@ -34,47 +34,84 @@
* General Public License plus this exception.
*/
#include "co_debug.h"
#include "co_log.h"
#include <stdarg.h>
#include <string.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)
{
time_t t;
struct tm* tm;
char Date[11], Time[11];
time(&t);
tm = localtime(&t);
time_t t = time(NULL);
struct tm* lt = localtime(&t);
if (fulldate) {
strftime(Date, 11, "%Y-%m-%d", tm);
strftime(Date, 11, "%Y-%m-%d", lt);
fprintf(stream, "%s ", Date);
}
strftime(Time, 11, "%H:%M:%S", tm);
strftime(Time, 11, "%H:%M:%S", lt);
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
print_time(stderr);
// 2. print filename only, without path
const char* file2 = strrchr(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
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, 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 @@
// TODO: The functionality of this file overlaps that of rt_errh.
#ifndef CO_DEBUG
#define CO_DEBUG
#ifndef CO_LOG_H
#define CO_LOG_H
#include <stdarg.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 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
\ No newline at end of file
......@@ -39,8 +39,6 @@
#ifndef CO_UNIQUE_PTR
#define CO_UNIQUE_PTR
#include "co_debug.h"
// std::unique_ptr for single objects -> free memory with 'delete'
template<typename T>
class unique_ptr {
......
......@@ -200,7 +200,7 @@ mkdir := mkdir
# Set to /buildversion for frozen dbs versions
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)
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,
int main(int argc, char* argv[])
{
setDebug(1);
log_setLevel(LOG_TRACE);
if (argc >= 2 && streq(argv[1], "-m")) {
wb_erep::printMethods();
exit(0);
......@@ -126,7 +126,7 @@ WbQt::WbQt(int argc, char* argv[])
strcpy(volumename, "directory");
sw_projectvolume = 1;
arg_cnt = 0;
debug_print("%s ", argv[0]);
log_debug("%s ", argv[0]);
for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
if (argv[i][0] == '-') {
......
......@@ -40,7 +40,7 @@
#include <stdlib.h>
#include <string.h>
#include "co_debug.h"
#include "co_log.h"
#include "cow_style_qt.h"
......@@ -79,7 +79,7 @@ CmdQt::CmdQt(int argc, char* argv[])
int main(int argc, char* argv[])
{
setDebug(1);
log_setLevel(LOG_TRACE);
QApplication app(argc, argv);
QApplication::setStyle(new PwrStyle());
new CmdQt(argc, argv);
......
......@@ -436,16 +436,16 @@ WAttText* WNavQt::watttext_new(
void WNavQt::wge_subwindow_loop(WGe* wge)
{
debug_print(" before wge_subwindow_loop\n");
log_debug(" before wge_subwindow_loop\n");
QApplication::exec();
debug_print(" after wge_subwindow_loop\n");
log_debug(" after wge_subwindow_loop\n");
}
void WNavQt::wge_modal_loop(WGe* wge)
{
debug_print(" before wge_modal_loop\n");
log_debug(" before wge_modal_loop\n");
QApplication::exec();
debug_print(" after wge_modal_loop\n");
log_debug(" after wge_modal_loop\n");
}
void WNavQt::message_dialog(char* title, char* text)
......
......@@ -56,7 +56,7 @@ void close_cb(void* ctx, void* xhelp)
int main(int argc, char* argv[])
{
setDebug(1);
log_setLevel(LOG_TRACE);
int sts;
QApplication app(argc, argv);
......@@ -80,7 +80,7 @@ CoHelpQt::CoHelpQt(int argc, char* argv[], int* return_sts)
{
pwr_tStatus sts;
debug_print("%s ", argv[0]);
log_debug("%s ", argv[0]);
for (int i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
}
......
......@@ -71,11 +71,11 @@ static void rtmon_close_cb(void* ctx)
int main(int argc, char* argv[])
{
setDebug(1);
log_setLevel(LOG_TRACE);
int sts;
char display[80] = "";
debug_print("%s ", argv[0]);
log_debug("%s ", argv[0]);
for (int i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
if (streq(argv[i], "-h")) {
......
......@@ -71,13 +71,13 @@ static void statusmon_close(void* ctx)
int main(int argc, char* argv[])
{
setDebug(1);
log_setLevel(LOG_TRACE);
int sts;
int mode = nodelist_eMode_SystemStatus;
int view_descr = 0;
char language[20] = "";
debug_print("%s ", argv[0]);
log_debug("%s ", argv[0]);
for (int i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
if (streq(argv[i], "-h")) {
......
......@@ -551,7 +551,7 @@ void XttQtWidget::valchanged_cmd_entry()
int main(int argc, char* argv[])
{
setDebug(1);
log_setLevel(LOG_TRACE);
int sts;
QApplication app(argc, argv);
......@@ -598,7 +598,7 @@ XttQt::XttQt(int argc, char* argv[], int* return_sts)
strcat(title, nodename);
}
debug_print("%s ", argv[0]);
log_debug("%s ", argv[0]);
for (int i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
}
......
......@@ -73,7 +73,7 @@ static int timeout_func()
int main(int argc, char* argv[])
{
setDebug(1);
log_setLevel(LOG_TRACE);
pwr_tStatus sts;
int i;
char str[256];
......@@ -85,7 +85,7 @@ int main(int argc, char* argv[])
/* If arguments, treat them as a command and then exit */
// Open directory volume as default
debug_print("%s ", argv[0]);
log_debug("%s ", argv[0]);
str[0] = 0;
for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
......
......@@ -64,7 +64,7 @@ static void usage()
int main(int argc, char* argv[])
{
setDebug(1);
log_setLevel(LOG_TRACE);
int i;
pwr_tFileName file;
char graph_name[80];
......@@ -88,7 +88,7 @@ int main(int argc, char* argv[])
CoXHelpQt* xhelp = new CoXHelpQt(0, xhelp_eUtility_Wtt, &sts);
CoXHelpQt::set_default(xhelp);
debug_print("%s ", argv[0]);
log_debug("%s ", argv[0]);
if (argc > 1) {
for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
......
......@@ -37,7 +37,7 @@
#ifndef QT_HELPERS_H
#define QT_HELPERS_H
#include "co_debug.h"
#include "co_log.h"
#include <QAction>
#include <QComboBox>
......
......@@ -434,7 +434,7 @@ void CoWowQtObject::DisplayLicense()
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;
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