Skip to content
Closed
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions base/hlog.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <time.h>

//#include "hmutex.h"
Expand All @@ -26,6 +27,30 @@
#define hmutex_unlock pthread_mutex_unlock
#endif

// Self-contained pid/tid for the %p/%t format specifiers. hlog is meant to be
// usable standalone (only depending on hexport.h), so we don't pull in
// hthread.h/hplatform.h here; instead inline the platform primitives, using
// the headers already included above (windows.h / pthread.h).
#ifdef _WIN32
#define hlog_getpid() (long)GetCurrentProcessId()
#define hlog_gettid() (long)GetCurrentThreadId()
#else
#include <unistd.h> // for getpid
#define hlog_getpid() (long)getpid()
#if defined(__linux__)
#include <sys/syscall.h> // for SYS_gettid
static inline long hlog_gettid() { return (long)syscall(SYS_gettid); }
#elif defined(__APPLE__)
static inline long hlog_gettid() {
uint64_t tid = 0;
pthread_threadid_np(NULL, &tid);
return (long)tid;
}
#else
#define hlog_gettid() (long)pthread_self()
#endif
#endif

//#include "htime.h"
#define SECONDS_PER_HOUR 3600
#define SECONDS_PER_DAY 86400 // 24*3600
Expand Down Expand Up @@ -478,6 +503,12 @@ int logger_print(logger_t* logger, int level, const char* fmt, ...) {
buf[len++] = plevel[i];
}
break;
case 'p':
len += snprintf(buf + len, bufsize - len, "%ld", hlog_getpid());
break;
case 't':
len += snprintf(buf + len, bufsize - len, "%ld", hlog_gettid());
break;
case 's':
{
va_list ap;
Expand Down
2 changes: 2 additions & 0 deletions base/hlog.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ HV_EXPORT void logger_set_level_by_str(logger_t* logger, const char* level);
* %Z us
* %l First character of level
* %L All characters of level
* %p pid (process id)
* %t tid (thread id)
* %s message
* %% %
*/
Expand Down
Loading