Skip to content

Commit

Permalink
Refactored logging. Increased default log buffer size.
Browse files Browse the repository at this point in the history
  • Loading branch information
troky committed Jun 10, 2014
1 parent d8d8ba8 commit 9f6bf16
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 109 deletions.
143 changes: 78 additions & 65 deletions logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,79 +47,92 @@ static void my_log_curses(int prio, const char *datetime, const char *str, bool
}
}

void applog(int prio, const char* fmt, ...)
{
va_list args;

va_start(args, fmt);
vapplogsiz(prio, LOGBUFSIZ, fmt, args);
va_end(args);
}

void applogsiz(int prio, int size, const char* fmt, ...)
{
va_list args;

va_start(args, fmt);
vapplogsiz(prio, size, fmt, args);
va_end(args);
}

/* high-level logging function, based on global opt_log_level */
void applog(int prio, const char* fmt, ...) {
va_list args;

if (opt_debug || prio != LOG_DEBUG) {
if (use_syslog || opt_log_output || prio <= opt_log_level) {
char tmp42[LOGBUFSIZ];
va_start(args, fmt);
vsnprintf(tmp42, sizeof(tmp42), fmt, args);
va_end(args);
_applog(prio, tmp42, false);
}
}
void vapplogsiz(int prio, int size, const char* fmt, va_list args)
{
if (opt_debug || prio != LOG_DEBUG) {
if (use_syslog || opt_log_output || prio <= opt_log_level) {
char *tmp42 = (char *)calloc(size, 1);
vsnprintf(tmp42, size, fmt, args);
_applog(prio, tmp42, false);
free(tmp42);
}
}
}

/*
* log function
*/
void _applog(int prio, const char *str, bool force)
{
#ifdef HAVE_SYSLOG_H
if (use_syslog) {
syslog(prio, "%s", str);
}
if (use_syslog) {
syslog(prio, "%s", str);
}
#else
if (0) {}
if (0) {}
#endif
else {
char datetime[64];
struct timeval tv = {0, 0};
struct tm *tm;

cgtime(&tv);

const time_t tmp_time = tv.tv_sec;
tm = localtime(&tmp_time);

/* Day changed. */
if (opt_log_show_date && (last_date_output_day != tm->tm_mday))
{
last_date_output_day = tm->tm_mday;
char date_output_str[64];
snprintf(date_output_str, sizeof(date_output_str), "Log date is now %d-%02d-%02d",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday);
_applog(prio, date_output_str, force);

}

if (opt_log_show_date)
{
snprintf(datetime, sizeof(datetime), "[%d-%02d-%02d %02d:%02d:%02d] ",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
}
else
{
snprintf(datetime, sizeof(datetime), "[%02d:%02d:%02d] ",
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
}

/* Only output to stderr if it's not going to the screen as well */
if (!isatty(fileno((FILE *)stderr))) {
fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
fflush(stderr);
}

my_log_curses(prio, datetime, str, force);
}
else {
char datetime[64];
struct timeval tv = {0, 0};
struct tm *tm;

cgtime(&tv);

const time_t tmp_time = tv.tv_sec;
tm = localtime(&tmp_time);

/* Day changed. */
if (opt_log_show_date && (last_date_output_day != tm->tm_mday)) {
last_date_output_day = tm->tm_mday;
char date_output_str[64];
snprintf(date_output_str, sizeof(date_output_str), "Log date is now %d-%02d-%02d",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday);
_applog(prio, date_output_str, force);
}

if (opt_log_show_date) {
snprintf(datetime, sizeof(datetime), "[%d-%02d-%02d %02d:%02d:%02d] ",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
}
else {
snprintf(datetime, sizeof(datetime), "[%02d:%02d:%02d] ",
tm->tm_hour,
tm->tm_min,
tm->tm_sec);
}

/* Only output to stderr if it's not going to the screen as well */
if (!isatty(fileno((FILE *)stderr))) {
fprintf(stderr, "%s%s\n", datetime, str); /* atomic write to stderr */
fflush(stderr);
}

my_log_curses(prio, datetime, str, force);
}
}
81 changes: 37 additions & 44 deletions logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,74 +28,67 @@ extern int opt_log_level;

extern int opt_log_show_date;

#define LOGBUFSIZ 256
#define LOGBUFSIZ 512

void applog(int prio, const char* fmt, ...);
void applogsiz(int prio, int size, const char* fmt, ...);
void vapplogsiz(int prio, int size, const char* fmt, va_list args);

extern void _applog(int prio, const char *str, bool force);

#define IN_FMT_FFL " in %s %s():%d"

#define applogsiz(prio, _SIZ, fmt, ...) do { \
if (opt_debug || prio != LOG_DEBUG) { \
if (use_syslog || opt_log_output || prio <= opt_log_level) { \
char tmp42[_SIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_applog(prio, tmp42, false); \
} \
} \
} while (0)

#define forcelog(prio, fmt, ...) do { \
if (opt_debug || prio != LOG_DEBUG) { \
if (use_syslog || opt_log_output || prio <= opt_log_level) { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_applog(prio, tmp42, true); \
} \
} \
if (opt_debug || prio != LOG_DEBUG) { \
if (use_syslog || opt_log_output || prio <= opt_log_level) { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_applog(prio, tmp42, true); \
} \
} \
} while (0)

#define quit(status, fmt, ...) do { \
if (fmt) { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_applog(LOG_ERR, tmp42, true); \
} \
_quit(status); \
if (fmt) { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_applog(LOG_ERR, tmp42, true); \
} \
_quit(status); \
} while (0)

#define quithere(status, fmt, ...) do { \
if (fmt) { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
##__VA_ARGS__, __FILE__, __func__, __LINE__); \
_applog(LOG_ERR, tmp42, true); \
} \
_quit(status); \
if (fmt) { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
##__VA_ARGS__, __FILE__, __func__, __LINE__); \
_applog(LOG_ERR, tmp42, true); \
} \
_quit(status); \
} while (0)

#define quitfrom(status, _file, _func, _line, fmt, ...) do { \
if (fmt) { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
##__VA_ARGS__, _file, _func, _line); \
_applog(LOG_ERR, tmp42, true); \
} \
_quit(status); \
if (fmt) { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt IN_FMT_FFL, \
##__VA_ARGS__, _file, _func, _line); \
_applog(LOG_ERR, tmp42, true); \
} \
_quit(status); \
} while (0)

#ifdef HAVE_CURSES

#define wlog(fmt, ...) do { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_wlog(tmp42); \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_wlog(tmp42); \
} while (0)

#define wlogprint(fmt, ...) do { \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_wlogprint(tmp42); \
char tmp42[LOGBUFSIZ]; \
snprintf(tmp42, sizeof(tmp42), fmt, ##__VA_ARGS__); \
_wlogprint(tmp42); \
} while (0)

#endif
Expand Down

5 comments on commit 9f6bf16

@mrbrdo
Copy link
Contributor

@mrbrdo mrbrdo commented on 9f6bf16 Jun 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
Just let's keep indentation changes in separate, isolated commits, please.

@troky
Copy link
Contributor Author

@troky troky commented on 9f6bf16 Jun 10, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

@evolvia31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, could you push this commit to master branch please ?

Thank you :o) you do a great job to sgminer.

@mrbrdo
Copy link
Contributor

@mrbrdo mrbrdo commented on 9f6bf16 Jun 13, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will be merging some of the working changes from v5_0 to master in about 2 weeks. Basically we will not be merging algorithm switching, since it is still buggy, but most of the other changes are ok.

@evolvia31
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok thanks to your reply. Do you think this refactored logging commit is OK to you ?? If you think this changes is stable, i will patch my sgminer with it even if you don't push it to master branch.

Please sign in to comment.