69 lines
1.7 KiB
C++
69 lines
1.7 KiB
C++
/*
|
|
* log4cpp.cpp
|
|
*
|
|
* Created by Bob Polis on 05-07-2007.
|
|
* Copyright 2007 Thalictrum. All rights reserved.
|
|
*/
|
|
|
|
#include "log4cpp.hpp"
|
|
|
|
// UNIX
|
|
#include <unistd.h>
|
|
#include <pthread.h>
|
|
#include <sys/time.h>
|
|
|
|
// C++ Standard Library
|
|
#include <cstdio>
|
|
#include <cstdarg>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <vector>
|
|
#include <mutex>
|
|
|
|
using namespace std;
|
|
|
|
stringstream glog;
|
|
pthread_mutex_t log4cpp_lock = PTHREAD_MUTEX_INITIALIZER;
|
|
mutex m;
|
|
|
|
void print_timestamp()
|
|
{
|
|
struct timeval secs;
|
|
(void)gettimeofday(&secs, NULL);
|
|
time_t now = time(NULL);
|
|
char dtstamp[32];
|
|
strftime(dtstamp, 32, "%F %T", localtime(&now));
|
|
long millisecs = (long)((double)secs.tv_usec / 1000.0 + 0.5) % 1000;
|
|
#if __APPLE__
|
|
fprintf(stderr, "%s.%03ld %s[%ld] ", dtstamp, millisecs, getprogname(), (long)getpid());
|
|
#elif __linux__
|
|
fprintf(stderr, "%s.%03ld %s[%ld] ", dtstamp, millisecs, program_invocation_name, (long)getpid());
|
|
#endif
|
|
}
|
|
|
|
void __slog(const char* func, const char* file, int line, stringstream& ss)
|
|
{
|
|
::pthread_mutex_lock(&log4cpp_lock);
|
|
print_timestamp();
|
|
string f(file);
|
|
cerr << "(" << f.substr(f.rfind('/') + 1) << ":" << line << ") " << func << "(): " << ss.str() << endl;
|
|
ss.str("");
|
|
::pthread_mutex_unlock(&log4cpp_lock);
|
|
}
|
|
|
|
void _dlog(const char* func, const char* file, int line, const char* fmt, ...) {
|
|
lock_guard<mutex> lock {m};
|
|
print_timestamp();
|
|
string f {file};
|
|
va_list args1;
|
|
va_start(args1, fmt);
|
|
va_list args2;
|
|
va_copy(args2, args1);
|
|
vector<char> buf(1 + vsnprintf(nullptr, 0, fmt, args1));
|
|
va_end(args1);
|
|
vsnprintf(buf.data(), buf.size(), fmt, args2);
|
|
va_end(args2);
|
|
cerr << "(" << f.substr(f.rfind('/') + 1) << ":" << line << ") " << func << "(): " << buf.data() << endl;
|
|
}
|