~swtor-meters-developers/swtor-meters/trunk

« back to all changes in this revision

Viewing changes to src/Logger.cpp

  • Committer: Thomas Lokshall
  • Date: 2012-12-13 14:28:05 UTC
  • Revision ID: thomas.lokshall@gmail.com-20121213142805-h42ikg5ikdr8bc5q
Initial commit for Qt version of the application. Currently only has a GUI with sample data.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/// @file
2
 
/// Implements the logging file writer.
3
 
 
4
 
#include "Logger.h"
5
 
 
6
 
using namespace swtor;
7
 
 
8
 
std::ofstream Logger::m_file;
9
 
 
10
 
bool Logger::Open(std::string filename)
11
 
{
12
 
    if (m_file.is_open())
13
 
        Close();
14
 
    m_file.open(filename.c_str(), std::ios::out | std::ios::app);
15
 
    WriteLine("Logging started");
16
 
    return m_file.is_open();
17
 
}
18
 
 
19
 
void Logger::Close()
20
 
{
21
 
    WriteLine("Logging ended");
22
 
    if (m_file.is_open())
23
 
        m_file.close();
24
 
}
25
 
 
26
 
void Logger::WriteLine(std::string line)
27
 
{
28
 
    if (m_file.is_open())
29
 
        m_file << GetDateTime() << " :: " << line << std::endl;
30
 
}
31
 
 
32
 
std::string Logger::GetDateTime()
33
 
{
34
 
    std::stringstream stream;
35
 
    time_t now = time(0);
36
 
    tm *ltm = localtime(&now);
37
 
    stream << 1900 + ltm->tm_year << "-";
38
 
    if (ltm->tm_mon < 11)
39
 
        stream << "0" << ltm->tm_mon + 1 << "-";
40
 
    else
41
 
        stream << ltm->tm_mon + 1 << "-";
42
 
    if (ltm->tm_mday < 10)
43
 
        stream << "0" << ltm->tm_mday << " ";
44
 
    else
45
 
        stream << ltm->tm_mday << " ";
46
 
    if (ltm->tm_hour < 10)
47
 
        stream << "0" << ltm->tm_hour << ":";
48
 
    else
49
 
        stream << ltm->tm_hour << ":";
50
 
    if (ltm->tm_min < 10)
51
 
        stream << "0" << ltm->tm_min << ":";
52
 
    else
53
 
        stream << ltm->tm_min << ":";
54
 
    if (ltm->tm_sec < 10)
55
 
        stream << "0" << ltm->tm_sec;
56
 
    else
57
 
        stream << ltm->tm_sec;
58
 
    return stream.str();
59
 
}