mirror of
https://github.com/ddnet/ddnet.git
synced 2024-09-20 09:34:19 +00:00
Threaded logger output
This commit is contained in:
parent
d44e6088bb
commit
49682f4a8d
|
@ -88,12 +88,36 @@ void dbg_break()
|
||||||
*((volatile unsigned*)0) = 0x0;
|
*((volatile unsigned*)0) = 0x0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define QUEUE_SIZE 16
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char q[QUEUE_SIZE][1024*4];
|
||||||
|
int begin;
|
||||||
|
int end;
|
||||||
|
LOCK mutex;
|
||||||
|
LOCK notempty;
|
||||||
|
LOCK notfull;
|
||||||
|
} Queue;
|
||||||
|
|
||||||
|
static int dbg_msg_threaded = 0;
|
||||||
|
static Queue log_queue;
|
||||||
|
|
||||||
|
int queue_empty(Queue *q)
|
||||||
|
{
|
||||||
|
return q->begin == q->end;
|
||||||
|
}
|
||||||
|
|
||||||
|
int queue_full(Queue *q)
|
||||||
|
{
|
||||||
|
return ((q->end+1) % QUEUE_SIZE) == q->begin;
|
||||||
|
}
|
||||||
|
|
||||||
void dbg_msg(const char *sys, const char *fmt, ...)
|
void dbg_msg(const char *sys, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
char str[1024*4];
|
|
||||||
char *msg;
|
char *msg;
|
||||||
int i, len;
|
int len;
|
||||||
|
|
||||||
//str_format(str, sizeof(str), "[%08x][%s]: ", (int)time(0), sys);
|
//str_format(str, sizeof(str), "[%08x][%s]: ", (int)time(0), sys);
|
||||||
time_t rawtime;
|
time_t rawtime;
|
||||||
|
@ -104,6 +128,39 @@ void dbg_msg(const char *sys, const char *fmt, ...)
|
||||||
timeinfo = localtime ( &rawtime );
|
timeinfo = localtime ( &rawtime );
|
||||||
|
|
||||||
strftime (timestr,sizeof(timestr),"%y-%m-%d %H:%M:%S",timeinfo);
|
strftime (timestr,sizeof(timestr),"%y-%m-%d %H:%M:%S",timeinfo);
|
||||||
|
|
||||||
|
if(dbg_msg_threaded)
|
||||||
|
{
|
||||||
|
lock_wait(log_queue.notfull);
|
||||||
|
lock_wait(log_queue.mutex);
|
||||||
|
int e = queue_empty(&log_queue);
|
||||||
|
|
||||||
|
str_format(log_queue.q[log_queue.end], sizeof(log_queue.q[log_queue.end]), "[%s][%s]: ", timestr, sys);
|
||||||
|
|
||||||
|
len = strlen(log_queue.q[log_queue.end]);
|
||||||
|
msg = (char *)log_queue.q[log_queue.end] + len;
|
||||||
|
|
||||||
|
va_start(args, fmt);
|
||||||
|
#if defined(CONF_FAMILY_WINDOWS)
|
||||||
|
_vsnprintf(msg, sizeof(log_queue.q[log_queue.end])-len, fmt, args);
|
||||||
|
#else
|
||||||
|
vsnprintf(msg, sizeof(log_queue.q[log_queue.end])-len, fmt, args);
|
||||||
|
#endif
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
log_queue.end = (log_queue.end + 1) % QUEUE_SIZE;
|
||||||
|
|
||||||
|
if(e)
|
||||||
|
lock_release(log_queue.notempty);
|
||||||
|
|
||||||
|
if(!queue_full(&log_queue))
|
||||||
|
lock_release(log_queue.notfull);
|
||||||
|
|
||||||
|
lock_release(log_queue.mutex);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
char str[1024*4];
|
||||||
str_format(str, sizeof(str), "[%s][%s]: ", timestr, sys);
|
str_format(str, sizeof(str), "[%s][%s]: ", timestr, sys);
|
||||||
|
|
||||||
len = strlen(str);
|
len = strlen(str);
|
||||||
|
@ -117,8 +174,55 @@ void dbg_msg(const char *sys, const char *fmt, ...)
|
||||||
#endif
|
#endif
|
||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
|
int i;
|
||||||
for(i = 0; i < num_loggers; i++)
|
for(i = 0; i < num_loggers; i++)
|
||||||
loggers[i](str);
|
loggers[i](str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dbg_msg_thread(void *v)
|
||||||
|
{
|
||||||
|
char str[1024*4];
|
||||||
|
while(1)
|
||||||
|
{
|
||||||
|
lock_wait(log_queue.notempty);
|
||||||
|
lock_wait(log_queue.mutex);
|
||||||
|
int f = queue_full(&log_queue);
|
||||||
|
|
||||||
|
str_copy(str, log_queue.q[log_queue.begin], sizeof(str));
|
||||||
|
|
||||||
|
log_queue.begin = (log_queue.begin + 1) % QUEUE_SIZE;
|
||||||
|
|
||||||
|
if(f)
|
||||||
|
lock_release(log_queue.notfull);
|
||||||
|
|
||||||
|
if(!queue_empty(&log_queue))
|
||||||
|
lock_release(log_queue.notempty);
|
||||||
|
|
||||||
|
lock_release(log_queue.mutex);
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for(i = 0; i < num_loggers; i++)
|
||||||
|
loggers[i](str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dbg_enable_threaded()
|
||||||
|
{
|
||||||
|
Queue *q = &log_queue;
|
||||||
|
q->begin = 0;
|
||||||
|
q->end = 0;
|
||||||
|
q->mutex = lock_create();
|
||||||
|
q->notempty = lock_create();
|
||||||
|
q->notfull = lock_create();
|
||||||
|
lock_wait(q->notempty);
|
||||||
|
|
||||||
|
dbg_msg_threaded = 1;
|
||||||
|
|
||||||
|
void *Thread = thread_create(dbg_msg_thread, 0);
|
||||||
|
#if defined(CONF_FAMILY_UNIX)
|
||||||
|
pthread_detach((pthread_t)Thread);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void logger_stdout(const char *line)
|
static void logger_stdout(const char *line)
|
||||||
|
|
|
@ -1176,6 +1176,7 @@ void swap_endian(void *data, unsigned elem_size, unsigned num);
|
||||||
typedef void (*DBG_LOGGER)(const char *line);
|
typedef void (*DBG_LOGGER)(const char *line);
|
||||||
void dbg_logger(DBG_LOGGER logger);
|
void dbg_logger(DBG_LOGGER logger);
|
||||||
|
|
||||||
|
void dbg_enable_threaded();
|
||||||
void dbg_logger_stdout();
|
void dbg_logger_stdout();
|
||||||
void dbg_logger_debugger();
|
void dbg_logger_debugger();
|
||||||
void dbg_logger_file(const char *filename);
|
void dbg_logger_file(const char *filename);
|
||||||
|
|
|
@ -3045,6 +3045,8 @@ int main(int argc, const char **argv) // ignore_convention
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
dbg_enable_threaded();
|
||||||
|
|
||||||
CClient *pClient = CreateClient();
|
CClient *pClient = CreateClient();
|
||||||
IKernel *pKernel = IKernel::Create();
|
IKernel *pKernel = IKernel::Create();
|
||||||
pKernel->RegisterInterface(pClient);
|
pKernel->RegisterInterface(pClient);
|
||||||
|
|
|
@ -1964,6 +1964,7 @@ static CServer *CreateServer() { return new CServer(); }
|
||||||
|
|
||||||
int main(int argc, const char **argv) // ignore_convention
|
int main(int argc, const char **argv) // ignore_convention
|
||||||
{
|
{
|
||||||
|
dbg_enable_threaded();
|
||||||
#if defined(CONF_FAMILY_WINDOWS)
|
#if defined(CONF_FAMILY_WINDOWS)
|
||||||
for(int i = 1; i < argc; i++) // ignore_convention
|
for(int i = 1; i < argc; i++) // ignore_convention
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue