CRASH: player initial phase missing
[Tsunagari.git] / src / log.cpp
blob0df4370b49869622fa0fa9e5f965347fb5409d49
1 /*********************************
2 ** Tsunagari Tile Engine **
3 ** log.cpp **
4 ** Copyright 2011-2012 OmegaSDG **
5 *********************************/
7 #include <iostream>
9 #include <Gosu/Timing.hpp>
11 #include "log.h"
12 #include "python.h"
13 #include "world.h"
15 #ifdef _WIN32
16 #include <windows.h>
17 #include "os-windows.h"
18 #endif
20 static verbosity_t verb = V_NORMAL;
22 static unsigned long startTime;
24 static std::string& chomp(std::string& str)
26 std::string::size_type notwhite = str.find_last_not_of(" \t\n\r");
27 str.erase(notwhite + 1);
28 return str;
31 static std::string ts()
33 unsigned long now = Gosu::milliseconds();
35 std::ostringstream ts;
36 ts.precision(3);
37 ts << std::fixed;
38 ts << (now - startTime) / (long double)1000.0;
39 return "[" + ts.str() + "] ";
42 bool Log::init()
44 startTime = Gosu::milliseconds();
45 return true;
48 void Log::setVerbosity(verbosity_t v)
50 verb = v;
53 void Log::info(std::string domain, std::string msg)
55 std::string str = ts() + "Info [" + domain + "] - " + chomp(msg);
56 if (verb > V_NORMAL)
57 std::cout << str << std::endl;
60 void Log::err(std::string domain, std::string msg)
62 std::string str = ts() + "Error [" + domain + "] - " + chomp(msg);
63 if (inPythonScript) {
64 PyErr_SetString(PyExc_RuntimeError, str.c_str());
65 boost::python::throw_error_already_set();
67 else {
68 if (verb > V_QUIET) {
69 std::cerr << str << std::endl;
70 #ifdef _WIN32
71 wMessageBox("Tsunagari - Error", str);
72 #endif
77 void Log::fatal(std::string domain, std::string msg)
79 std::string str = ts() + "Fatal [" + domain + "] - " + chomp(msg);
80 if (inPythonScript) {
81 PyErr_SetString(PyExc_RuntimeError, str.c_str());
82 boost::python::throw_error_already_set();
84 else {
85 std::cerr << str << std::endl;
86 #ifdef _WIN32
87 wMessageBox("Tsunagari - Fatal", str);
88 #endif
92 void Log::reportVerbosityOnStartup()
94 std::string verbString;
95 switch (conf.verbosity)
97 case V_QUIET:
98 verbString = "QUIET";
99 break;
100 case V_NORMAL:
101 verbString = "NORMAL";
102 break;
103 case V_VERBOSE:
104 verbString = "VERBOSE";
105 break;
107 std::cout << ts() << "Reporting engine messages in " << verbString
108 << " mode." << std::endl;
111 static void pythonLogInfo(std::string msg)
113 Log::info("Script", msg);
116 void exportLog()
118 using namespace boost::python;
120 pythonAddFunction("log", pythonLogInfo);