Replace log4cpp with custom log function

Also prints a simpler error to stderr for WARN or above so that
we print something on the commandline if something go wrong.
This commit is contained in:
Alexander Larsson 2010-06-28 21:27:10 +02:00
parent b08b80d13f
commit 7e26ff3c26
11 changed files with 102 additions and 73 deletions

View File

@ -43,10 +43,9 @@
#include "tunnel_channel.h"
#include "rect.h"
#include "gui/gui.h"
#include <log4cpp/BasicConfigurator.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#define STICKY_KEY_PIXMAP ALT_IMAGE_RES_ID
#define STICKY_KEY_TIMEOUT 750
@ -2096,30 +2095,84 @@ bool Application::process_cmd_line(int argc, char** argv)
return true;
}
#ifdef RED_DEBUG
static unsigned int log_level = LOG_DEBUG;
#else
static unsigned int log_level = LOG_INFO;
#endif
static FILE *log_file = NULL;
void spice_log_cleanup(void)
{
if (log_file) {
fclose(log_file);
log_file = NULL;
}
}
static inline std::string function_to_func_name(const std::string& f_name)
{
#ifdef __GNUC__
std::string name(f_name);
std::string::size_type end_pos = f_name.find('(');
if (end_pos == std::string::npos) {
return f_name;
}
std::string::size_type start = f_name.rfind(' ', end_pos);
if (start == std::string::npos) {
return name.substr(0, end_pos);
}
end_pos -= ++start;
return name.substr(start, end_pos);
#else
return f_name;
#endif
}
void spice_log(unsigned int type, const char *function, const char *format, ...)
{
std::string formated_message;
va_list ap;
const char *type_as_char[] = { "DEBUG", "INFO", "WARN", "ERROR", "FATAL" };
const char *type_as_nice_char[] = { "Debug", "Info", "Warning", "Error", "Fatal error" };
if (type < log_level) {
return;
}
assert(type <= LOG_FATAL);
va_start(ap, format);
string_vprintf(formated_message, format, ap);
va_end(ap);
if (type >= log_level && log_file != NULL) {
fprintf(log_file, "%ld %s [%llu:%llu] %s: %s\n", (long)time(NULL), type_as_char[type],
(long long int)Platform::get_process_id(), (long long int)Platform::get_thread_id(),
function_to_func_name(function).c_str(),
formated_message.c_str());
fflush(log_file);
}
if (type >= LOG_WARN) {
fprintf(stderr, "%s: %s\n", type_as_nice_char[type], formated_message.c_str());
}
}
void Application::init_logger()
{
std::string log_file_name;
Platform::get_app_data_dir(log_file_name, app_name);
Platform::path_append(log_file_name, "spicec.log");
int fd = ::open(log_file_name.c_str(), O_CREAT | O_APPEND | O_WRONLY, 0644);
log_file = ::fopen(log_file_name.c_str(), "a");
if (fd == -1) {
log4cpp::BasicConfigurator::configure();
if (log_file == NULL) {
fprintf(stderr, "Failed to open log file %s\n", log_file_name.c_str());
return;
}
log4cpp::Category& root = log4cpp::Category::getRoot();
#ifdef RED_DEBUG
root.setPriority(log4cpp::Priority::DEBUG);
root.removeAllAppenders();
root.addAppender(new log4cpp::FileAppender("_", fd));
#else
root.setPriority(log4cpp::Priority::INFO);
root.removeAllAppenders();
::close(fd);
root.addAppender(new log4cpp::RollingFileAppender("_", log_file_name));
#endif
}
void Application::init_globals()

View File

@ -21,9 +21,6 @@
#include <stdlib.h>
#include <sstream>
#include <log4cpp/Category.hh>
#include <log4cpp/convenience.h>
#include "platform.h"
#ifdef WIN32
@ -51,49 +48,36 @@
#endif
#ifdef __GNUC__
static inline std::string pretty_func_to_func_name(const std::string& f_name)
{
std::string name(f_name);
std::string::size_type end_pos = f_name.find('(');
if (end_pos == std::string::npos) {
return f_name;
}
std::string::size_type start = f_name.rfind(' ', end_pos);
if (start == std::string::npos) {
return f_name;
}
end_pos -= ++start;
return name.substr(start, end_pos);
}
enum {
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL
};
#define FUNC_NAME pretty_func_to_func_name(__PRETTY_FUNCTION__).c_str()
void spice_log(unsigned int type, const char *function, const char *format, ...);
void spice_log_cleanup(void);
#ifdef __GNUC__
#define SPICE_FUNC_NAME __PRETTY_FUNCTION__
#else
#define FUNC_NAME __FUNCTION__
#define SPICE_FUNC_NAME __FUNCTION__
#endif
#define LOGGER_SECTION(section) LOG4CPP_LOGGER(section)
#define LOG(type, format, ...) spice_log(type, SPICE_FUNC_NAME, format, ## __VA_ARGS__)
LOG4CPP_LOGGER("spice")
#define LOG(type, format, ...) { \
std::string log_message; \
string_printf(log_message, "[%llu:%llu] %s: " format, Platform::get_process_id(), \
Platform::get_thread_id(), FUNC_NAME, ## __VA_ARGS__); \
LOG4CPP_##type(logger, log_message.c_str()); \
}
#define LOG_INFO(format, ...) LOG(INFO, format, ## __VA_ARGS__)
#define LOG_WARN(format, ...) LOG(WARN, format, ## __VA_ARGS__)
#define LOG_ERROR(format, ...) LOG(ERROR, format, ## __VA_ARGS__)
#define LOG_INFO(format, ...) LOG(LOG_INFO, format, ## __VA_ARGS__)
#define LOG_WARN(format, ...) LOG(LOG_WARN, format, ## __VA_ARGS__)
#define LOG_ERROR(format, ...) LOG(LOG_ERROR, format, ## __VA_ARGS__)
#define PANIC(format, ...) { \
LOG(FATAL, format, ## __VA_ARGS__); \
LOG(LOG_FATAL, format, ## __VA_ARGS__); \
ON_PANIC(); \
}
#define PANIC_ON(x) if ((x)) { \
LOG(FATAL, "%s panic %s\n", __FUNCTION__, #x); \
LOG(LOG_FATAL, "%s panic %s\n", __FUNCTION__, #x); \
ON_PANIC(); \
}
@ -101,7 +85,7 @@ LOG4CPP_LOGGER("spice")
#define DBG(level, format, ...) { \
if (level <= DBGLEVEL) { \
LOG(DEBUG, format, ## __VA_ARGS__); \
LOG(LOG_DEBUG, format, ## __VA_ARGS__); \
} \
}

View File

@ -59,8 +59,8 @@ public:
enum MessageType {
QUESTION,
INFO,
WARNINNG,
ERROR,
WARNING,
ERROR_MSG
};
struct ButtonInfo {

View File

@ -92,7 +92,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
exit_val = SPICEC_ERROR_CODE_ERROR;
}
log4cpp::Category::shutdown();
spice_log_cleanup();
pthread_win32_process_detach_np();
return exit_val;

View File

@ -20,6 +20,7 @@
#include <shlobj.h>
#include <io.h>
#include <conio.h>
#include <fcntl.h>
#include "platform.h"
#include "win_platform.h"

View File

@ -47,7 +47,7 @@
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories=".;..;..\..\common;..\..\..\spice-protocol;..\..\common\win;&quot;..\..\common\win\my_getopt-1.5&quot;;&quot;$(SPICE_LIBS)\include&quot;;&quot;$(SPICE_LIBS)\include\pixman-1&quot;;&quot;$(SPICE_LIBS)\include\CEGUI-0.6.2&quot;"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;SW_CANVAS_CACHE;RED_DEBUG;SW_CANVAS_NO_CHUNKS;_WIN32_WINNT=0x0500;LOG4CPLUS_STATIC;USE_GLZ;PTW32_STATIC_LIB;CEGUI_STATIC"
PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;SW_CANVAS_CACHE;RED_DEBUG;SW_CANVAS_NO_CHUNKS;_WIN32_WINNT=0x0500;USE_GLZ;PTW32_STATIC_LIB;CEGUI_STATIC"
MinimalRebuild="false"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
@ -69,7 +69,7 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="log4cppD.lib pixman-1D.lib libeay32MTd.lib ssleay32MTd.lib ws2_32.lib msimg32.lib winmm.lib libcelt_0_5_1D.lib pthreadVC2d.lib version.lib CEGUIBase_Static_d.lib CEGUITGAImageCodec_Static_d.lib CEGUIExpatParser_Static_d.lib freetype2312MT_D.lib libexpatMT_D.lib pcre_D.lib CEGUIFalagardWRBase_Static_d.lib libjpeg-static-mt-debug.lib zlibwapi.lib"
AdditionalDependencies="pixman-1D.lib libeay32MTd.lib ssleay32MTd.lib ws2_32.lib msimg32.lib winmm.lib libcelt_0_5_1D.lib pthreadVC2d.lib version.lib CEGUIBase_Static_d.lib CEGUITGAImageCodec_Static_d.lib CEGUIExpatParser_Static_d.lib freetype2312MT_D.lib libexpatMT_D.lib pcre_D.lib CEGUIFalagardWRBase_Static_d.lib libjpeg-static-mt-debug.lib zlibwapi.lib"
OutputFile="$(OutDir)\spicec.exe"
LinkIncremental="2"
AdditionalLibraryDirectories="&quot;$(SPICE_LIBS)\lib&quot;"
@ -129,7 +129,7 @@
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories=".;..;..\..\..\spice-protocol;..\..\common;..\..\common\win;&quot;..\..\common\win\my_getopt-1.5&quot;;&quot;$(SPICE_LIBS)\include&quot;;&quot;$(SPICE_LIBS)\include\pixman-1&quot;;&quot;$(SPICE_LIBS)\include\CEGUI-0.6.2&quot;"
PreprocessorDefinitions="WIN32;_WINDOWS;SW_CANVAS_CACHE;SW_CANVAS_NO_CHUNKS;_WIN32_WINNT=0x0500;LOG4CPLUS_STATIC;USE_GLZ;PTW32_STATIC_LIB;CEGUI_STATIC"
PreprocessorDefinitions="WIN32;_WINDOWS;SW_CANVAS_CACHE;SW_CANVAS_NO_CHUNKS;_WIN32_WINNT=0x0500;USE_GLZ;PTW32_STATIC_LIB;CEGUI_STATIC"
RuntimeLibrary="0"
UsePrecompiledHeader="0"
WarningLevel="3"
@ -148,7 +148,7 @@
/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="log4cpp.lib pixman-1.lib libeay32MT.lib ssleay32MT.lib ws2_32.lib msimg32.lib winmm.lib libcelt_0_5_1.lib pthreadVC2.lib version.lib CEGUIBase_Static.lib CEGUITGAImageCodec_Static.lib CEGUIExpatParser_Static.lib freetype2312MT.lib libexpatMT.lib pcre.lib CEGUIFalagardWRBase_Static.lib libjpeg-static-mt.lib zlibwapi.lib"
AdditionalDependencies="pixman-1.lib libeay32MT.lib ssleay32MT.lib ws2_32.lib msimg32.lib winmm.lib libcelt_0_5_1.lib pthreadVC2.lib version.lib CEGUIBase_Static.lib CEGUITGAImageCodec_Static.lib CEGUIExpatParser_Static.lib freetype2312MT.lib libexpatMT.lib pcre.lib CEGUIFalagardWRBase_Static.lib libjpeg-static-mt.lib zlibwapi.lib"
OutputFile="$(OutDir)\spicec.exe"
LinkIncremental="1"
AdditionalLibraryDirectories="&quot;$(SPICE_LIBS)\lib&quot;"

View File

@ -19,7 +19,6 @@ INCLUDES = \
$(GL_CFLAGS) \
$(ALSA_CFLAGS) \
$(PIXMAN_CFLAGS) \
$(LOG4CPP_CFLAGS) \
$(CELT051_CFLAGS) \
$(SSL_CFLAGS) \
$(XRANDR_CFLAGS) \
@ -173,7 +172,6 @@ spicec_SOURCES = \
spicec_LDFLAGS = \
$(SPICEC_STATIC_LINKAGE_BSTATIC) \
$(LOG4CPP_LIBS) \
$(CELT051_LIBS) \
$(SSL_LIBS) \
$(CEGUI_LIBS) \

View File

@ -21,7 +21,7 @@
static void cleanup()
{
log4cpp::Category::shutdown();
spice_log_cleanup();
}
const char * version_str = VERSION;

View File

@ -33,6 +33,7 @@
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <set>

View File

@ -107,8 +107,6 @@ dnl Check deps
PKG_CHECK_MODULES(PROTOCOL, spice-protocol)
AC_SUBST(PROTOCOL_CFLAGS)
SPICE_REQUIRES+=" log4cpp"
AC_CHECK_LIBM
AC_SUBST(LIBM)
@ -123,11 +121,6 @@ SPICE_NONPKGCONFIG_LIBS+=" -pthread $LIBM $LIBRT"
SPICE_REQUIRES=""
PKG_CHECK_MODULES(LOG4CPP, log4cpp)
AC_SUBST(LOG4CPP_CFLAGS)
AC_SUBST(LOG4CPP_LIBS)
SPICE_REQUIRES+=" log4cpp"
PKG_CHECK_MODULES(CEGUI, CEGUI >= 0.6.0 CEGUI < 0.7.0)
AC_SUBST(CEGUI_CFLAGS)
AC_SUBST(CEGUI_LIBS)

View File

@ -8,7 +8,6 @@ INCLUDES = \
$(JPEG_CFLAGS) \
$(PIXMAN_CFLAGS) \
$(GL_CFLAGS) \
$(LOG4CPP_CFLAGS) \
$(SSL_CFLAGS) \
$(CELT051_CFLAGS) \
$(SLIRP_CFLAGS) \