summaryrefslogtreecommitdiff
path: root/shared/last_error.cpp
blob: 57053c3060915f7d06b7ee7d0d5fe935902dd8b8 (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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) 2008-2011 ZenJu (zhnmju123 AT gmx.de)                    *
// **************************************************************************

#include "last_error.h"
#include "string_utf8.h"
#include "i18n.h"

#ifdef FFS_WIN
#include <wx/msw/wrapwin.h> //includes "windows.h"

#elif defined FFS_LINUX
#include <cstring>
#include <cerrno>
#endif

using namespace zen;


#ifdef FFS_WIN
std::wstring zen::getLastErrorFormatted(unsigned long lastError) //try to get additional Windows error information
{
    //determine error code if none was specified
    if (lastError == 0)
        lastError = ::GetLastError();

    std::wstring output = _("Windows Error Code %x:");
    replace(output, L"%x", toString<std::wstring>(lastError));

    LPWSTR buffer = NULL;
    if (::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM    |
                        FORMAT_MESSAGE_MAX_WIDTH_MASK |
                        FORMAT_MESSAGE_IGNORE_INSERTS | //important: without this flag ::FormatMessage() will fail if message contains placeholders
                        FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, lastError, 0, reinterpret_cast<LPWSTR>(&buffer), 0, NULL) != 0)
    {
        if (buffer) //just to be sure
        {
            output += L" ";
            output += buffer;
            ::LocalFree(buffer);
        }
    }
    ::SetLastError(lastError); //restore last error
    return output;
}

#elif defined FFS_LINUX
std::wstring zen::getLastErrorFormatted(int lastError) //try to get additional Linux error information
{
    //determine error code if none was specified
    if (lastError == 0)
        lastError = errno; //don't use "::", errno is a macro!

    std::wstring output = _("Linux Error Code %x:");
    replace(output, L"%x", toString<std::wstring>(lastError));

    output += L" ";
    output += utf8CvrtTo<std::wstring>(::strerror(lastError));

    errno = lastError; //restore errno
    return output;
}
#endif
bgstack15