[gnote] Add log to file capability in debug mode



commit 59e61f057419b132fc7b44c64d0c0dd4251f98f3
Author: Aurimas Äernius <aurisc4 gmail com>
Date:   Sun Jun 17 19:53:27 2012 +0300

    Add log to file capability in debug mode

 src/debug.cpp |   43 +++++++++++++++++++++++++++++++++++++------
 src/debug.hpp |   17 +++++++++++++++++
 2 files changed, 54 insertions(+), 6 deletions(-)
---
diff --git a/src/debug.cpp b/src/debug.cpp
index 40724ee..44b0217 100644
--- a/src/debug.cpp
+++ b/src/debug.cpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2012 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -40,6 +41,7 @@
 #include <string.h>
 #include <stdarg.h>
 
+#include <glibmm.h>
 #include <pthread.h>
 
 #if defined(NDEBUG)
@@ -60,6 +62,8 @@ namespace utils {
 
   static void _vprint(const char *prefix, const char *fmt, 
                      const char* func,  va_list marker);
+  static void _vfprint(FILE *file, const char *prefix, const char *fmt, 
+                     const char* func,  va_list marker);
   static void _print(const char *prefix, const char *fmt, 
              const char* func, ...);
 
@@ -108,6 +112,27 @@ namespace utils {
   }
 
 
+  void log_print(const char *fmt, const char *func, ...)
+  {
+#ifdef DEBUG
+#define LOG_MSG "LOG: "
+    Glib::ustring filename = Glib::build_filename(Glib::get_home_dir(), "gnote.log");
+    FILE *file = fopen(filename.c_str(), "a");
+    if(!file) {
+      return;
+    }
+    va_list marker;
+
+    va_start(marker, func);
+    _vfprint(file, LOG_MSG, fmt, func, marker);
+
+   va_end(marker);
+   fclose(file);
+#undef LOG_MSG
+#endif
+  }
+
+
   static void _print(const char *prefix, const char *fmt, 
               const char* func, ...)
   {
@@ -123,20 +148,26 @@ namespace utils {
   static void _vprint(const char *prefix, const char *fmt, 
               const char* func,  va_list marker)
   {
+    _vfprint(stderr, prefix, fmt, func, marker);
+  }
+
+  static void _vfprint(FILE *file, const char *prefix, const char *fmt, 
+                     const char* func,  va_list marker)
+  {
 //    static boost::recursive_mutex mutex;
 //    boost::recursive_mutex::scoped_lock lock(mutex);
     char buf[128];
     snprintf(buf, 128, "(%lu) ", (pthread_t)pthread_self());
-    fwrite(buf, 1, strlen(buf), stderr);
-    fwrite(prefix, 1, strlen(prefix), stderr);
+    fwrite(buf, 1, strlen(buf), file);
+    fwrite(prefix, 1, strlen(prefix), file);
 
     if(func) {
-      fwrite(func, 1, strlen(func), stderr);
-      fwrite(" - ", 1, 3, stderr);
+      fwrite(func, 1, strlen(func), file);
+      fwrite(" - ", 1, 3, file);
     }
 
-    vfprintf(stderr, fmt, marker);
-    fprintf(stderr, "\n");
+    vfprintf(file, fmt, marker);
+    fprintf(file, "\n");
   }
 
 }
diff --git a/src/debug.hpp b/src/debug.hpp
index d1d857e..5c1a05f 100644
--- a/src/debug.hpp
+++ b/src/debug.hpp
@@ -1,6 +1,7 @@
 /*
  * gnote
  *
+ * Copyright (C) 2012 Aurimas Cernius
  * Copyright (C) 2009 Hubert Figuiere
  *
  * This program is free software: you can redistribute it and/or modify
@@ -49,6 +50,14 @@ namespace utils {
 #define ERR_OUT(x, ...) \
   ::utils::err_print(x,  __FUNCTION__, ## __VA_ARGS__)
 
+#ifdef DEBUG
+#define LOG_OUT(x, ...) \
+  ::utils::log_print(x, __FUNCTION__, ## __VA_ARGS__)
+#else
+#define LOG_OUT(x, ...)
+#endif
+
+
 
   /** print debug messages. printf format.
    * NOOP if DEBUG is not defined.
@@ -77,6 +86,14 @@ namespace utils {
   void err_print(const char *fmt, const char* func, ...)
     _PRINTF_FORMAT(1,3);
 
+  /** print message to log file.
+   * Call with LOG_OUT macro.
+   * @param fmt the format string, printf style
+   * @param func the function name
+   */
+  void log_print(const char *fmt, const char *func, ...)
+    _PRINTF_FORMAT(1,3);
+
 }
 
 #endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]