summaryrefslogtreecommitdiff
path: root/riff/Logger.cpp
blob: a54e44751805abe3f64855cb7762eda32d5a64a6 (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
74
75
76
77
78
79
80
81
82
83
84
85
/*
 * Logger.cpp
 *
 * Copyright (C) ST-Ericsson SA 2011
 * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
 *          Ola Borgelin <ola.borgelin@stericsson.com>,
 *          Karin Hedlund <karin.hedlund@stericsson.com>,
 *          Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
 * License terms: 3-clause BSD license
 *
 */

/*
 * @addtogroup Logger
 * @{
 */

#include "Logger.h"
#include <string>
#include <cstdio>
#include <ctime>
#include <cstdarg>
#include <cstddef>
using namespace std;

bool Logger::verbose_;

Logger::Logger(const string& name):
        name_(name)
{
    console_ = stdout;

}

Logger::~Logger()
{
}

void Logger::log(LogSeverity severity, const char* format, ...) const
{
    time_t t = time(0);
    tm lt;
    localtime_r(&t, &lt);

    if (verbose_ || severity == Logger::PROGRESS || severity == Logger::ERROR) {
        flockfile(console_);
        fprintf(console_, "%02d:%02d:%02d ", lt.tm_hour, lt.tm_min, lt.tm_sec);

        fprintf(console_, " - ");

        va_list args;
        va_start(args, format);
        vfprintf(console_, format, args);
        va_end(args);
        fprintf(console_, "\n");
        fflush(console_);
        funlockfile(console_);

    }
}

void Logger::hex(const unsigned char* buffer, size_t size) const
{
    flockfile(console_);

    for (size_t i = 0; i < size; i++) {
        fprintf(console_, "%02x ", buffer[i]);

        if ((i + 1) % 16 == 0 || (i + 1) == size)
            fprintf(console_, "\n");
    }

    fflush(console_);
    funlockfile(console_);
}

void Logger::logRaw(const char* message) const
{
    flockfile(console_);
    fprintf(console_, "%s", message);
    fflush(console_);
    funlockfile(console_);
}

/* @} */