Merge branch 'master' of ssh://main.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / lib / log.cc
index 7f1eea20634f8cba480d8d6884765f7e1be14268..a0b031589bf1836080c98c170c2fde1860df8dfe 100644 (file)
  *  @brief A very simple logging class.
  */
 
-#include <fstream>
 #include <time.h>
+#include <cstdio>
 #include "log.h"
+#include "cross.h"
+
+#include "i18n.h"
 
 using namespace std;
 
 Log::Log ()
-       : _level (VERBOSE)
+       : _level (STANDARD)
 {
 
 }
@@ -42,16 +45,33 @@ Log::log (string m, Level l)
        if (l > _level) {
                return;
        }
-       
+
        time_t t;
        time (&t);
        string a = ctime (&t);
 
        stringstream s;
-       s << a.substr (0, a.length() - 1) << ": " << m;
+       s << a.substr (0, a.length() - 1) << N_(": ") << m;
        do_log (s.str ());
 }
 
+void
+Log::microsecond_log (string m, Level l)
+{
+       boost::mutex::scoped_lock lm (_mutex);
+
+       if (l > _level) {
+               return;
+       }
+
+       struct timeval tv;
+       gettimeofday (&tv, 0);
+
+       stringstream s;
+       s << tv.tv_sec << N_(":") << tv.tv_usec << N_(" ") << m;
+       do_log (s.str ());
+}      
+
 void
 Log::set_level (Level l)
 {
@@ -59,9 +79,22 @@ Log::set_level (Level l)
        _level = l;
 }
 
+void
+Log::set_level (string l)
+{
+       if (l == N_("verbose")) {
+               set_level (VERBOSE);
+               return;
+       } else if (l == N_("timing")) {
+               set_level (TIMING);
+               return;
+       }
+
+       set_level (STANDARD);
+}
 
 /** @param file Filename to write log to */
-FileLog::FileLog (string file)
+FileLog::FileLog (boost::filesystem::path file)
        : _file (file)
 {
 
@@ -70,7 +103,13 @@ FileLog::FileLog (string file)
 void
 FileLog::do_log (string m)
 {
-       ofstream f (_file.c_str(), fstream::app);
-       f << m << "\n";
+       FILE* f = fopen_boost (_file, "a");
+       if (!f) {
+               cout << "(could not log to " << _file.string() << "): " << m << "\n";
+               return;
+       }
+
+       fprintf (f, "%s\n", m.c_str ());
+       fclose (f);
 }