summaryrefslogtreecommitdiff
path: root/cpp/src/Ice/LoggerI.cpp
blob: b5e599cc6389d98a7efc238222030a2adf19405f (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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceUtil/Time.h>
#include <Ice/LoggerI.h>
#include <IceUtil/Mutex.h>
#include <IceUtil/MutexPtrLock.h>

#ifdef _WIN32
#  include <IceUtil/StringUtil.h>
#  include <IceUtil/ScopedArray.h>
#  include <IceUtil/StringConverter.h>
#endif

#include <Ice/LocalException.h>

using namespace std;
using namespace Ice;
using namespace IceInternal;

namespace
{

IceUtil::Mutex* outputMutex = 0;

class Init
{
public:

    Init()
    {
        outputMutex = new IceUtil::Mutex;
    }

    ~Init()
    {
        delete outputMutex;
        outputMutex = 0;
    }
};

Init init;

#ifdef _WIN32
string
UTF8ToCodePage(const string& in, int codePage)
{
    string out;
    if(!in.empty())
    {
        if(CP_UTF8 == codePage)
        {
            out = in;
        }
        else
        {
            IceUtil::ScopedArray<wchar_t> wbuffer;
            int size = 0;
            int wlength = 0;
            do
            {
                size == 0 ? 2 * in.size() : 2 * size;
                wbuffer.reset(new wchar_t[size]);
                wlength = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, in.c_str(), -1, wbuffer.get(), size);
            }
            while(wlength == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);

            if(wlength == 0)
            {
                throw IceUtil::IllegalConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString());
            }

            //
            // WC_ERR_INVALID_CHARS conversion flag is only supported with 65001 (UTF-8) and
            // 54936 (GB18030 Simplified Chinese)
            //
            DWORD conversionFlags = (codePage == 65001 || codePage == 54936) ? WC_ERR_INVALID_CHARS : 0;

            IceUtil::ScopedArray<char> buffer;

            size = 0;
            int length = 0;
            do
            {
                size == 0 ? wlength + 2 : 2 * size;
                buffer.reset(new char[length]);
                length = WideCharToMultiByte(codePage, conversionFlags, wbuffer.get(), wlength, buffer.get(), size, 0, 0);
            }
            while(length == 0 && GetLastError() == ERROR_INSUFFICIENT_BUFFER);

            if(!length)
            {
                throw IceUtil::IllegalConversionException(__FILE__, __LINE__, IceUtilInternal::lastErrorToString());;
            }
            out.assign(buffer.get(), length);
        }
    }
    return out;
}
#endif

}

Ice::LoggerI::LoggerI(const string& prefix, const string& file, 
                      bool convert, const IceUtil::StringConverterPtr& converter) :
    _convert(convert),
    _converter(converter)
{
    if(!prefix.empty())
    {
        _prefix = prefix + ": ";
    }

    if(!file.empty())
    {
        _file = file;
        _out.open(file, fstream::out | fstream::app);
        if(!_out.is_open())
        {
            throw InitializationException(__FILE__, __LINE__, "FileLogger: cannot open " + _file);
        }
    }
}


Ice::LoggerI::~LoggerI()
{
    if(_out.is_open())
    {
        _out.close();
    }
}

void
Ice::LoggerI::print(const string& message)
{
    write(message, false);
}

void
Ice::LoggerI::trace(const string& category, const string& message)
{
    string s = "-- " + IceUtil::Time::now().toDateTime() + " " + _prefix;
    if(!category.empty())
    {
        s += category + ": ";
    }
    s += message;

    write(s, true);
}

void
Ice::LoggerI::warning(const string& message)
{
    write("-! " + IceUtil::Time::now().toDateTime() + " " + _prefix + "warning: " + message, true);
}

void
Ice::LoggerI::error(const string& message)
{
    write("!! " + IceUtil::Time::now().toDateTime() + " " + _prefix + "error: " + message, true);
}

LoggerPtr
Ice::LoggerI::cloneWithPrefix(const std::string& prefix)
{
    return new LoggerI(prefix, _file, _convert, _converter);
}

void
Ice::LoggerI::write(const string& message, bool indent)
{
    IceUtilInternal::MutexPtrLock<IceUtil::Mutex> sync(outputMutex);

    string s = message;

    if(indent)
    {
        string::size_type idx = 0;
        while((idx = s.find("\n", idx)) != string::npos)
        {
            s.insert(idx + 1, "   ");
            ++idx;
        }
    }

    if(_out.is_open())
    {
        _out << s << endl;
    }
    else
    {
#ifndef _WIN32
        cerr << s << endl;
#else
        //
        // Convert the message from the native narrow string encoding to the console
        // code page encoding for printing. If the _convert member is set to false 
        // we don't do any conversion.
        //
        if(!_convert)
        {
            cerr << s << endl;
        }
        else
        {
            try
            {
                //
                // First we convert the message to UTF8 using nativeToUTF8
                // then we convert the message to the console code page
                // using UTF8ToCodePage.
                //
                // nativeToUTF8 doesn't do any conversion if the converter is
                // null likewise UTF8ToCodePage doesn't do any conversion if
                // the code page is UTF8.
                //
                // We cannot use cerr here as writing to console using cerr
                // will do its own conversion and will corrupt the messages.
                //
                fprintf_s(stderr, "%s\n", UTF8ToCodePage(IceUtil::nativeToUTF8(_converter, s), 
                                                         GetConsoleOutputCP()).c_str());
            }
            catch(const IceUtil::IllegalConversionException&)
            {
                //
                // If there is a problem with the encoding conversions we just
                // write the original message without encoding conversions.
                //
                fprintf_s(stderr, "%s\n", s.c_str());
            }
        }
#endif
    }
}