Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yffbit committed May 29, 2024
1 parent 44d7c62 commit a1aab79
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 2 additions & 3 deletions include/tools/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ class Logger {
Logger(bool cmd, const char *path, const char *mode = "w", bool timestamp = false, bool new_line = true, int period = 10)
:cmd(cmd), timestamp(timestamp), new_line(new_line), period(period) {
if(path) {
errno_t err = fopen_s(&file, path, mode);
if(err) printf("%d\n", err);
if(!file) printf("create file %s failed\n", path);
file = fopen(path, mode);
if(!file) printf("failed to create file %s\n", path);
}
}
virtual ~Logger() {
Expand Down
6 changes: 5 additions & 1 deletion mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class QLogger : public Logger {
QLogger(const char *path, const char *mode = "w", bool timestamp = false, int period = 10):Logger(false, path, mode, timestamp, true, period) {}
virtual void log(const char *format, ...) {
if(timestamp) log_time();
va_list args = nullptr;
va_list args;
va_start(args, format);
if(file) {
vfprintf(file, format, args);
Expand All @@ -29,6 +29,10 @@ class QLogger : public Logger {
fflush(file);
}
if(new_line) fprintf(file, "\n");
#ifdef __GNUC__
va_end(args);
va_start(args, format);
#endif
}
// qDebug().noquote() << QString::vasprintf(QObject::tr(format).toLocal8Bit(), args);
qDebug().noquote() << QString::vasprintf(QObject::tr(format).toStdString().c_str(), args);
Expand Down
14 changes: 12 additions & 2 deletions src/tools/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ void get_localtime(char *buf, size_t n, const char *format) {
// time(&now);
int ms = duration_cast<milliseconds>(tp.time_since_epoch()).count() - now * 1000;
tm tm_now;
#ifdef _MSC_VER
localtime_s(&tm_now, &now);
#else
localtime_r(&now, &tm_now);
#endif
// strftime(buf, n, format, &tm_now);
sprintf_s(buf, n, format, tm_now.tm_year+1900, tm_now.tm_mon+1, tm_now.tm_mday,
snprintf(buf, n, format, tm_now.tm_year+1900, tm_now.tm_mon+1, tm_now.tm_mday,
tm_now.tm_hour, tm_now.tm_min, tm_now.tm_sec, ms);
}

Expand All @@ -22,7 +26,7 @@ string get_localtime() {

void Logger::log(const char *format, ...) {
if(timestamp) log_time();
va_list args = nullptr;
va_list args;
va_start(args, format);
if(file) {
vfprintf(file, format, args);
Expand All @@ -31,6 +35,12 @@ void Logger::log(const char *format, ...) {
fflush(file);
}
if(new_line) fprintf(file, "\n");
#ifdef __GNUC__
if(cmd) {
va_end(args);
va_start(args, format);
}
#endif
}
if(cmd) {
vprintf(format, args);
Expand Down

0 comments on commit a1aab79

Please sign in to comment.