RingDebug.h 3.84 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 19
/***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux                                *
* Author: Jger Nicolas <nicolas.jager@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/>.   *
**************************************************************************/
#pragma once

atraczyk's avatar
atraczyk committed
20 21
#define UWP_DBG_CLIENT      0
#define UWP_DBG_VS          1
atraczyk's avatar
atraczyk committed
22
#define UWP_DBG_FILE        1
atraczyk's avatar
atraczyk committed
23

24 25
using namespace Windows::Storage;

Nicolas Jager's avatar
Nicolas Jager committed
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
namespace RingClientUWP
{

/* forward declaration */
ref class RingDebug;

/* delegate */
delegate void debugMessageToScreen(Platform::String^ message);

/* this is how to implement a singleton class*/
public ref class RingDebug sealed
{
public:
    /* singleton */
    static property RingDebug^ instance
    {
        RingDebug^ get()
        {
            static RingDebug^ instance_ = ref new RingDebug();
            return instance_;
        }
    }

    /* properties */

    /* functions */
internal:
atraczyk's avatar
atraczyk committed
53 54 55 56 57 58
    enum class Type { DMN, MSG, WNG, ERR };
    void print(const std::string& message, const Type& type,
                        std::string file, int line);
    void print(String^ message, const Type& type,
                        std::string file, int line);
    void print(Exception^ e, std::string file, int line);
59

Nicolas Jager's avatar
Nicolas Jager committed
60 61 62 63
    /* event */
    event debugMessageToScreen^ messageToScreen;

private:
64
    RingDebug(); // singleton
Nicolas Jager's avatar
Nicolas Jager committed
65 66
};

atraczyk's avatar
atraczyk committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80
void WriteLine(String^ str)
{
    std::wstringstream wStringstream;
    wStringstream << str->Data() << "\n";
    OutputDebugString(wStringstream.str().c_str());
}

void WriteException(Exception^ ex)
{
    std::wstringstream wStringstream;
    wStringstream << "0x" << ex->HResult << ": " << ex->Message->Data();
    OutputDebugString(wStringstream.str().c_str());
}

atraczyk's avatar
atraczyk committed
81 82 83 84 85 86 87 88
#define DMSG_(str) CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, \
ref new DispatchedHandler([=]() { RingDebug::instance->print(str, RingDebug::Type::DMN, __FILE__, __LINE__); }))

#define MSG_(str) CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, \
ref new DispatchedHandler([=]() { RingDebug::instance->print(str, RingDebug::Type::MSG, __FILE__, __LINE__); }))

#define WNG_(str) CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, \
ref new DispatchedHandler([=]() { RingDebug::instance->print(str, RingDebug::Type::WNG, __FILE__, __LINE__); }))
Nicolas Jager's avatar
Nicolas Jager committed
89

atraczyk's avatar
atraczyk committed
90 91
#define ERR_(str) CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, \
ref new DispatchedHandler([=]() { RingDebug::instance->print(str, RingDebug::Type::ERR, __FILE__, __LINE__); }))
Nicolas Jager's avatar
Nicolas Jager committed
92

atraczyk's avatar
atraczyk committed
93 94
#define EXC_(e) CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::High, \
ref new DispatchedHandler([=]() { RingDebug::instance->print(e, __FILE__, __LINE__); }))
Nicolas Jager's avatar
Nicolas Jager committed
95

Nicolas Jager's avatar
Nicolas Jager committed
96
}