~michihenning/storage-framework/stand-alone-provider-headers

« back to all changes in this revision

Viewing changes to src/internal/TraceMessageHandler.cpp

  • Committer: Michi Henning
  • Date: 2016-08-23 23:45:34 UTC
  • mfrom: (53.1.4 devel)
  • Revision ID: michi.henning@canonical.com-20160823234534-4owayhnhcp67g39q
Merged devel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 Canonical Ltd
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors: Michi Henning <michi.henning@canonical.com>
 
17
 */
 
18
 
 
19
#include <unity/storage/internal/TraceMessageHandler.h>
 
20
 
 
21
#include <chrono>
 
22
#include <mutex>
 
23
 
 
24
using namespace std;
 
25
 
 
26
namespace unity
 
27
{
 
28
namespace storage
 
29
{
 
30
namespace internal
 
31
{
 
32
namespace
 
33
{
 
34
 
 
35
string prefix;
 
36
 
 
37
void trace_message_handler(QtMsgType type, const QMessageLogContext& /*context*/, const QString& msg)
 
38
{
 
39
    using namespace std;
 
40
    using namespace std::chrono;
 
41
 
 
42
    static recursive_mutex mutex;
 
43
    lock_guard<decltype(mutex)> lock(mutex);
 
44
 
 
45
    auto now = system_clock::now();
 
46
    auto sys_time = system_clock::to_time_t(now);
 
47
    struct tm local_time;
 
48
    localtime_r(&sys_time, &local_time);
 
49
    int msecs = duration_cast<milliseconds>(now.time_since_epoch()).count() % 1000;
 
50
 
 
51
    if (!prefix.empty())
 
52
    {
 
53
        fprintf(stderr, "%s: ", prefix.c_str());
 
54
    }
 
55
    char buf[100];
 
56
    strftime(buf, sizeof(buf), "%T", &local_time);
 
57
    fprintf(stderr, "[%s.%03d]", buf, msecs);
 
58
    switch (type)
 
59
    {
 
60
        case QtWarningMsg:
 
61
            fputs(" Warning:", stderr);
 
62
            break;
 
63
        case QtCriticalMsg:
 
64
            fputs(" Critical:", stderr);
 
65
            break;
 
66
        // LCOV_EXCL_START
 
67
        case QtFatalMsg:
 
68
            fputs(" Fatal:", stderr);
 
69
            break;
 
70
        // LCOV_EXCL_STOP
 
71
        default:
 
72
            break;  // No label for debug messages.
 
73
    }
 
74
    fprintf(stderr, " %s\n", msg.toLocal8Bit().constData());
 
75
    if (type == QtFatalMsg)
 
76
    {
 
77
        abort();  // LCOV_EXCL_LINE
 
78
    }
 
79
}
 
80
 
 
81
}  // namespace
 
82
 
 
83
TraceMessageHandler::TraceMessageHandler(string const& prog_name)
 
84
{
 
85
    prefix = prog_name;
 
86
    old_message_handler_ = qInstallMessageHandler(trace_message_handler);
 
87
}
 
88
 
 
89
TraceMessageHandler::~TraceMessageHandler()
 
90
{
 
91
    qInstallMessageHandler(old_message_handler_);
 
92
}
 
93
 
 
94
}  // namespace internal
 
95
}  // namespace storage
 
96
}  // namespace unity