Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print timestamps in logs #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sockssrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ struct thread {
/* we log to stderr because it's not using line buffering, i.e. malloc which would need
locking when called from different threads. for the same reason we use dprintf,
which writes directly to an fd. */
#define dolog(...) do { if(!quiet) dprintf(2, __VA_ARGS__); } while(0)
#define LOGTS() { \
char t[20] = {}; struct tm tm_buf; time_t secs = time(NULL); \
strftime(t, sizeof(t), "[%m-%d %T] ", localtime_r(&secs, &tm_buf)); \
fputs(t, stderr); \
Copy link
Owner

Choose a reason for hiding this comment

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

you should use dprintf here too as buffered io isn't threadsafe - at least on glibc. mixing buffered and non-buffered makes the problem even worse.

Copy link
Owner

Choose a reason for hiding this comment

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

also, LOGTS should be a function to prevent all the code from be expanded at every callsite.

Copy link

Choose a reason for hiding this comment

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

Please use %Y-%m-%d %T for less ambiguity too.

}
#define dolog(...) do { if(quiet) break; LOGTS(); dprintf(2, __VA_ARGS__); } while(0)
#else
static void dolog(const char* fmt, ...) { }
#endif
Expand Down