Merge master.
[dcpomatic.git] / src / lib / log.cc
index accf3694d3775d24609c0149658ab7f76b5a7d87..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;
 
-/** @param f Filename to write log to */
-Log::Log (string f)
-       : _file (f)
-       , _level (VERBOSE)
+Log::Log ()
+       : _level (STANDARD)
 {
 
 }
@@ -44,16 +45,33 @@ Log::log (string m, Level l)
        if (l > _level) {
                return;
        }
-       
-       ofstream f (_file.c_str(), fstream::app);
 
        time_t t;
        time (&t);
        string a = ctime (&t);
-       
-       f << a.substr (0, a.length() - 1) << ": " << m << "\n";
+
+       stringstream s;
+       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)
 {
@@ -61,3 +79,37 @@ 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 (boost::filesystem::path file)
+       : _file (file)
+{
+
+}
+
+void
+FileLog::do_log (string m)
+{
+       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);
+}
+