log.hxx 4.48 KB
Newer Older
BlackAngle233's avatar
BlackAngle233 committed
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//=============================================================================================================================
//
// EasyAR Sense 4.2.0.8700-7bcbc8b1c
// Copyright (c) 2015-2021 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved.
// EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China
// and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd.
//
//=============================================================================================================================

#ifndef __EASYAR_LOG_HXX__
#define __EASYAR_LOG_HXX__

#include "easyar/types.hxx"

namespace easyar {

/// <summary>
/// Log class.
/// It is used to setup a custom log output function.
/// </summary>
class Log
{
public:
    /// <summary>
    /// Sets custom log output function. The log output function should be thread-safe.
    /// </summary>
    static void setLogFunc(FunctorOfVoidFromLogLevelAndString func);
    /// <summary>
    /// Sets custom log output function and run it in scheduler.
    /// </summary>
    static void setLogFuncWithScheduler(CallbackScheduler * scheduler, FunctorOfVoidFromLogLevelAndString func);
    /// <summary>
    /// Clears custom log output function and reverts to default log output function.
    /// </summary>
    static void resetLogFunc();
};

#ifndef __EASYAR_FUNCTOROFVOIDFROMLOGLEVELANDSTRING__
#define __EASYAR_FUNCTOROFVOIDFROMLOGLEVELANDSTRING__
struct FunctorOfVoidFromLogLevelAndString
{
    void * _state;
    void (* func)(void * _state, LogLevel, String *);
    void (* destroy)(void * _state);
    FunctorOfVoidFromLogLevelAndString(void * _state, void (* func)(void * _state, LogLevel, String *), void (* destroy)(void * _state));
};

static void FunctorOfVoidFromLogLevelAndString_func(void * _state, easyar_LogLevel, easyar_String *, /* OUT */ easyar_String * * _exception);
static void FunctorOfVoidFromLogLevelAndString_destroy(void * _state);
static inline easyar_FunctorOfVoidFromLogLevelAndString FunctorOfVoidFromLogLevelAndString_to_c(FunctorOfVoidFromLogLevelAndString f);
#endif

}

#endif

#ifndef __IMPLEMENTATION_EASYAR_LOG_HXX__
#define __IMPLEMENTATION_EASYAR_LOG_HXX__

#include "easyar/log.h"
#include "easyar/callbackscheduler.hxx"

namespace easyar {

inline void Log::setLogFunc(FunctorOfVoidFromLogLevelAndString arg0)
{
    easyar_Log_setLogFunc(FunctorOfVoidFromLogLevelAndString_to_c(arg0));
}
inline void Log::setLogFuncWithScheduler(CallbackScheduler * arg0, FunctorOfVoidFromLogLevelAndString arg1)
{
    easyar_Log_setLogFuncWithScheduler(arg0->get_cdata(), FunctorOfVoidFromLogLevelAndString_to_c(arg1));
}
inline void Log::resetLogFunc()
{
    easyar_Log_resetLogFunc();
}

#ifndef __IMPLEMENTATION_EASYAR_FUNCTOROFVOIDFROMLOGLEVELANDSTRING__
#define __IMPLEMENTATION_EASYAR_FUNCTOROFVOIDFROMLOGLEVELANDSTRING__
inline FunctorOfVoidFromLogLevelAndString::FunctorOfVoidFromLogLevelAndString(void * _state, void (* func)(void * _state, LogLevel, String *), void (* destroy)(void * _state))
{
    this->_state = _state;
    this->func = func;
    this->destroy = destroy;
}
static void FunctorOfVoidFromLogLevelAndString_func(void * _state, easyar_LogLevel arg0, easyar_String * arg1, /* OUT */ easyar_String * * _exception)
{
    *_exception = NULL;
    try {
        LogLevel cpparg0 = static_cast<LogLevel>(arg0);
        easyar_String_copy(arg1, &arg1);
        String * cpparg1 = new String(arg1);
        FunctorOfVoidFromLogLevelAndString * f = reinterpret_cast<FunctorOfVoidFromLogLevelAndString *>(_state);
        f->func(f->_state, cpparg0, cpparg1);
        delete cpparg1;
    } catch (std::exception & ex) {
        easyar_String_from_utf8_begin(ex.what(), _exception);
    }
}
static void FunctorOfVoidFromLogLevelAndString_destroy(void * _state)
{
    FunctorOfVoidFromLogLevelAndString * f = reinterpret_cast<FunctorOfVoidFromLogLevelAndString *>(_state);
    if (f->destroy) {
        f->destroy(f->_state);
    }
    delete f;
}
static inline easyar_FunctorOfVoidFromLogLevelAndString FunctorOfVoidFromLogLevelAndString_to_c(FunctorOfVoidFromLogLevelAndString f)
{
    easyar_FunctorOfVoidFromLogLevelAndString _return_value_ = {NULL, NULL, NULL};
    _return_value_._state = new FunctorOfVoidFromLogLevelAndString(f._state, f.func, f.destroy);
    _return_value_.func = FunctorOfVoidFromLogLevelAndString_func;
    _return_value_.destroy = FunctorOfVoidFromLogLevelAndString_destroy;
    return _return_value_;
}
#endif

}

#endif