RingDebug.cpp 5.26 KB
Newer Older
Nicolas Jager's avatar
Nicolas Jager committed
1
/***************************************************************************
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * Copyright (C) 2016 by Savoir-faire Linux                                *
 * Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com>              *
 * Author: Traczyk Andreas <andreas.traczyk@savoirfairelinux.com>          *
 *                                                                         *
 * This program is free software; you can redistribute it and/or modify    *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program is distributed in the hope that it will be useful,         *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License       *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/
Nicolas Jager's avatar
Nicolas Jager committed
19 20 21 22 23 24 25 26

/* client */
#include "pch.h"

using namespace RingClientUWP;

using namespace Platform;
using namespace Windows::UI::Core;
27
using namespace Windows::Storage;
Nicolas Jager's avatar
Nicolas Jager committed
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
using namespace std::chrono;

std::string
getDebugHeader(std::string file, int line)
{
    auto tid = std::this_thread::get_id();

    seconds s = duration_cast< seconds >(
        system_clock::now().time_since_epoch()
    );
    milliseconds ms = duration_cast< milliseconds >(
        system_clock::now().time_since_epoch()
    );

    static uint64_t secs = s.count();
    static uint64_t millis = ms.count() - (secs * 1000);

    std::ostringstream out;
    const auto prev_fill = out.fill();
    out << '[' << secs
        << '.' << std::right << std::setw(3) << std::setfill('0') << millis << std::left
        << '|' << std::right << std::setw(5) << std::setfill(' ') << tid << std::left;
    out.fill(prev_fill);
    out << "|" << std::setw(32) << (file + ':' + Utils::toString((line.ToString())));
    out << "] ";

    return out.str();
}

Nicolas Jager's avatar
Nicolas Jager committed
58
void
59 60
RingDebug::print(const  std::string& message, const Type& type,
                        std::string file, int line)
Nicolas Jager's avatar
Nicolas Jager committed
61
{
62
    std::string _message;
Nicolas Jager's avatar
Nicolas Jager committed
63

64 65 66 67 68 69
    if (type != Type::DMN)
        _message = getDebugHeader(file, line) + message;
    else
        _message = message;

    std::wstring wString = std::wstring(_message.begin(), _message.end());
Nicolas Jager's avatar
Nicolas Jager committed
70 71

    /* set message type. */
72
    wString = (type>Type::WNG)?(L"(EE) "):((type>Type::MSG)?(L"(WW) "):(L"")) + wString;
Nicolas Jager's avatar
Nicolas Jager committed
73

74
    String^ msg;
75
#if UWP_DBG_VS
Nicolas Jager's avatar
Nicolas Jager committed
76
    /* screen it into VS debug console */
77
    OutputDebugString((wString + L"\n").c_str());
78 79 80 81
#endif

#if UWP_DBG_CLIENT
    /* fire the event. */
82
    msg = ref new String(wString.c_str(), wString.length());
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    messageToScreen(msg);
#endif

#if UWP_DBG_FILE
    /* output to file */
    std::ofstream ofs;
    ofs.open (RingD::instance->getLocalFolder() + "debug.log", std::ofstream::out | std::ofstream::app);
    ofs << Utils::toString(msg) << "\n";
    ofs.close();
#endif
}

void
RingDebug::print(String^ message, const Type& type,
                        std::string file, int line)
{
    /* add header */
    auto messageTimestamped = Utils::toPlatformString(getDebugHeader(file, line)) + message;

    /* set message type. */
    messageTimestamped = (type>Type::WNG)?("(EE) "):((type>Type::MSG)?("(WW) "):("")) + messageTimestamped;
Nicolas Jager's avatar
Nicolas Jager committed
104

105 106 107 108 109 110 111 112
#if UWP_DBG_VS
    /* screen it into VS debug console */
    std::wstringstream wStringstream;
    wStringstream << messageTimestamped->Data() << "\n";
    OutputDebugString(wStringstream.str().c_str());
#endif

#if UWP_DBG_CLIENT
Nicolas Jager's avatar
Nicolas Jager committed
113
    /* fire the event. */
114 115
    messageToScreen(messageTimestamped);
#endif
116

117 118
#if UWP_DBG_FILE
    /* output to file */
119
    std::ofstream ofs;
120 121
    ofs.open (RingD::instance->getLocalFolder() + "debug.log", std::ofstream::out | std::ofstream::app);
    ofs << Utils::toString(messageTimestamped) << "\n";
122
    ofs.close();
123
#endif
124 125
}

126 127
void
RingDebug::print(Exception^ e, std::string file, int line)
128
{
129 130 131 132 133
    /* add header */
    auto message = Utils::toPlatformString(getDebugHeader(file, line)) + "0x" + e->HResult.ToString() + ": " + e->Message;

#if UWP_DBG_VS
    /* screen it into VS debug console */
134
    std::wstringstream wStringstream;
135
    wStringstream << message->Data() << "\n";
136
    OutputDebugString(wStringstream.str().c_str());
137 138 139 140 141 142 143 144 145 146 147 148 149 150
#endif

#if UWP_DBG_CLIENT
    /* fire the event. */
    messageToScreen(message);
#endif

#if UWP_DBG_FILE
    /* output to file */
    std::ofstream ofs;
    ofs.open (RingD::instance->getLocalFolder() + "debug.log", std::ofstream::out | std::ofstream::app);
    ofs << Utils::toString(message) << "\n";
    ofs.close();
#endif
151 152
}

153
RingDebug::RingDebug()
154
{
155 156 157 158
    /* clean the log file */
    std::ofstream ofs;
    ofs.open (RingD::instance->getLocalFolder() + "debug.log", std::ofstream::out | std::ofstream::trunc);
    ofs.close();
Nicolas Jager's avatar
Nicolas Jager committed
159
}
160