aboutsummaryrefslogtreecommitdiff
path: root/Logger.cpp
blob: 142442e82d651c910c2b9bd422f29e4fb8ce7a65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "Logger.hpp"
#include "ExitException.hpp"
#include <sstream>

using std::endl;
using std::cout;
using std::cerr;
using std::flush;
using std::hex;
using std::uppercase;
using std::ostringstream;

bool CLogger::mLogInfo = false;
int CLogger::mProgressLastPercent = -1;

void
CLogger::error(const string & msg, int returnValue)
{
    cerr << "ERROR:  " << msg << endl;
    throw CExitException(msg, returnValue);
}

void
CLogger::warning(const string & msg)
{
    cerr << "WARNING:  " << msg << endl; 
}

void
CLogger::info(const string & msg)
{
    if (!CLogger::mLogInfo)
 	return;
    cout << "INFO:  " << msg << endl; 
}

void
CLogger::progress(uint32_t b, uint32_t n)
{
    if (b > n)
        b = n;

    double d = n;
    int p = (int) (b / (d / 100.0));
    if ((p == 100) && (b != n))
        p = 99;
    else if (p > 100)
        p = 100;

    if (((p == 0) && (p != mProgressLastPercent))
        || ((p == 100) && (p != mProgressLastPercent))
        || ((p - mProgressLastPercent) >= LOGGER_PROGRESS_UNIT)) {
        cout << p << "% (" << b << " B / " << n << " B)" << endl;
        mProgressLastPercent = p;
    }
    
    if (p == 100)
        mProgressLastPercent = -1;
}

void
CLogger::setLogInfo(bool b)
{
    mLogInfo = b;
}

string
CLogger::decToHex(int dec)
{
    ostringstream os;
    os << "0x" << hex << uppercase << dec;
    return os.str();
}