summaryrefslogtreecommitdiff
path: root/FreeFileSync/Source/status_handler.cpp
blob: 6530ed48d3b218ef89c5b6cad48e8eea64a3ce51 (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
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0          *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************

#include "status_handler.h"
#include <zen/basic_math.h>
#include <zen/process_exec.h>

using namespace zen;


namespace
{
std::chrono::steady_clock::time_point lastExec;
}


bool fff::uiUpdateDue(bool force)
{
    const auto now = std::chrono::steady_clock::now();

    if (now >= lastExec + UI_UPDATE_INTERVAL || force)
    {
        lastExec = now;
        return true;
    }
    return false;
}


void fff::runCommandAndLogErrors(const Zstring& cmdLine, ErrorLog& errorLog)
{
    try
    {
        //give consoleExecute() some "time to fail", but not too long to hang our process
        const int DEFAULT_APP_TIMEOUT_MS = 100;

        if (const auto& [exitCode, output] = consoleExecute(cmdLine, DEFAULT_APP_TIMEOUT_MS); //throw SysError, SysErrorTimeOut
            exitCode != 0)
            throw SysError(formatSystemError("", replaceCpy(_("Exit code %x"), L"%x", numberTo<std::wstring>(exitCode)), utfTo<std::wstring>(output)));

        logMsg(errorLog, _("Executing command:") + L' ' + utfTo<std::wstring>(cmdLine) + L" [" + replaceCpy(_("Exit code %x"), L"%x", L"0") + L']', MSG_TYPE_INFO);
    }
    catch (SysErrorTimeOut&) //child process not failed yet => probably fine :>
    {
        logMsg(errorLog, _("Executing command:") + L' ' + utfTo<std::wstring>(cmdLine), MSG_TYPE_INFO);
    }
    catch (const SysError& e)
    {
        logMsg(errorLog, replaceCpy(_("Command %x failed."), L"%x", fmtPath(cmdLine)) + L"\n\n" + e.toString(), MSG_TYPE_ERROR);
    }
}


void fff::delayAndCountDown(std::chrono::steady_clock::time_point delayUntil, const std::function<void(const std::wstring& timeRemMsg)>& notifyStatus)
{
    assert(notifyStatus);
    if (notifyStatus)
        for (auto now = std::chrono::steady_clock::now(); now < delayUntil; now = std::chrono::steady_clock::now())
        {
            const auto timeRemMs = std::chrono::duration_cast<std::chrono::milliseconds>(delayUntil - now).count();
            notifyStatus(_P("1 sec", "%x sec", numeric::intDivCeil(timeRemMs, 1000)));

            std::this_thread::sleep_for(UI_UPDATE_INTERVAL / 2);
        }
    else
        std::this_thread::sleep_until(delayUntil);
}
bgstack15