Skip to content

Commit 01baa7a

Browse files
committed
crontab logging improvement
1 parent 1f1367d commit 01baa7a

File tree

6 files changed

+70
-28
lines changed

6 files changed

+70
-28
lines changed

CommandTrayHost/configure.cpp

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,7 +1878,7 @@ void handle_crontab(int idx)
18781878
if (to_start)
18791879
{
18801880
config_i_ref["enabled"] = true;
1881-
create_process(config_i_ref, ghJob);
1881+
create_process(config_i_ref, ghJob, true);
18821882
log_msg = "method:start started.";
18831883
//crontab_write_log = true;
18841884
}
@@ -1893,13 +1893,13 @@ void handle_crontab(int idx)
18931893
if (config_i_ref["enabled"] && config_i_ref["running"] && config_i_ref["en_job"])
18941894
{
18951895
disable_enable_menu(config_i_ref, ghJob);
1896-
log_msg = "method:kill stopped.";
1896+
log_msg = "method:stop killed.";
18971897
//crontab_write_log = true;
18981898
}
18991899
if (crontab_method == "restart")
19001900
{
19011901
config_i_ref["enabled"] = true;
1902-
create_process(config_i_ref, ghJob, false);
1902+
create_process(config_i_ref, ghJob, false, true);
19031903
log_msg = "method:restart done.";
19041904
//crontab_write_log = true;
19051905
}
@@ -1939,12 +1939,12 @@ void handle_crontab(int idx)
19391939
else
19401940
{
19411941
crontab_ref["enabled"] = false;
1942-
log_cron_msg = "count limited";
1942+
log_cron_msg = "count limited,stopped.";
19431943
log_count = -1;
19441944
}
19451945
if (json_object_has_member(crontab_ref, "log"))
19461946
{
1947-
crontab_log(config_i_ref, log_time_cur, log_time_next, log_msg, log_cron_msg, log_count);
1947+
crontab_log(crontab_ref, log_time_cur, log_time_next, config_i_ref["name"].get<std::string>().c_str(), log_msg, log_cron_msg, log_count, 0);
19481948
}
19491949
}
19501950
else
@@ -2248,7 +2248,8 @@ void create_process(
22482248
nlohmann::json& jsp, // we may update js
22492249
//int cmd_idx,
22502250
const HANDLE& ghJob,
2251-
bool runas_admin
2251+
bool runas_admin,
2252+
bool log_crontab
22522253
)
22532254
{
22542255
//必须先用wstring接住,不然作用域会消失
@@ -2442,6 +2443,20 @@ void create_process(
24422443
}
24432444
}
24442445

2446+
if (log_crontab)
2447+
{
2448+
auto& crontab_ref = jsp["crontab_config"];
2449+
const size_t buffer_len = 256;
2450+
char buffer[buffer_len];
2451+
size_t idx = 0, len;
2452+
printf_to_bufferA(buffer, buffer_len - idx, idx,
2453+
"si.wShowWindow:%s start_show:%s",
2454+
si.wShowWindow == SW_SHOW ? "SW_SHOW" : "SW_HIDE",
2455+
start_show ? "true" : "false"
2456+
);
2457+
crontab_log(crontab_ref, 0, 0, jsp["name"].get<std::string>().c_str(), buffer, "", 0, 1);
2458+
}
2459+
24452460
// https://stackoverflow.com/questions/53208/how-do-i-automatically-destroy-child-processes-in-windows
24462461
// Launch child process - example is notepad.exe
24472462
if (false == require_admin && CreateProcess(NULL, commandLine, NULL, NULL, FALSE, CREATE_NEW_CONSOLE | CREATE_BREAKAWAY_FROM_JOB, NULL, working_directory, &si, &pi))
@@ -2891,7 +2906,7 @@ BOOL IsMyProgramRegisteredForStartup(PCWSTR pszAppName)
28912906
lResult = RegGetValue(hKey, NULL, pszAppName, RRF_RT_REG_SZ, &dwRegType, szPathToExe_reg, &dwSize);
28922907
#endif
28932908
fSuccess = (lResult == ERROR_SUCCESS);
2894-
}
2909+
}
28952910

28962911
if (fSuccess)
28972912
{

CommandTrayHost/configure.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void get_command_submenu(std::vector<HMENU>&);
3131
int init_global(HANDLE&, HICON&);
3232
bool init_cth_path();
3333

34-
void create_process(nlohmann::json& jsp, const HANDLE&, bool runas_admin = false);
34+
void create_process(nlohmann::json& jsp, const HANDLE&, bool runas_admin = false, bool log_crontab = false);
3535
void show_hide_toggle(nlohmann::json& jsp);
3636
void disable_enable_menu(nlohmann::json& jsp, HANDLE, bool runas_admin = false);
3737

CommandTrayHost/utils.cpp

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -301,39 +301,66 @@ void rotate_file(PCWSTR filename)
301301
msg_prompt(L"There are too many log files, Please delete or move them elsewhere.", L"Logrotate error", MB_OK);
302302
}
303303

304-
void crontab_log(const nlohmann::json& jsp,
304+
/*
305+
* Make sure jsp has "crontab_config", before call crontab_log
306+
* @param time_cur,current time.
307+
* @param time_next,next schedule time. If both are 0, just output log_msg & cron_msg
308+
*/
309+
void crontab_log(const nlohmann::json& jsp_crontab_config,
305310
time_t time_cur,
306311
time_t time_next,
312+
PCSTR name,
307313
PCSTR log_msg,
308314
PCSTR cron_msg,
309-
int log_count
315+
int log_count,
316+
int log_level_limit
310317
)
311318
{
312-
if (json_object_has_member(jsp, "crontab_config"))
319+
//if (json_object_has_member(jsp, "crontab_config"))
313320
{
314-
auto& crontab_config_ref = jsp["crontab_config"];
321+
//auto& crontab_config_ref = jsp["crontab_config"];
322+
int log_level = jsp_crontab_config["log_level"];
323+
if (log_level < log_level_limit) { return; }
315324
const size_t buffer_len = 256;
316325
char buffer[buffer_len];
317326
size_t idx = 0, len;
318327
tm t1;
328+
bool is_crontab_trigger_msg = true;
329+
if (time_cur == 0)
330+
{
331+
time_cur = time(NULL);
332+
if (0 == time_next)is_crontab_trigger_msg = false;
333+
}
319334
localtime_s(&t1, &time_cur);
320335
idx = strftime(buffer, ARRAYSIZE(buffer), "%Y-%m-%d %H:%M:%S ", &t1);
321-
printf_to_bufferA(buffer, buffer_len - idx, idx,
322-
"[%s] [%s] [left count: %d] [%s]",
323-
jsp["name"].get<std::string>().c_str(),
324-
log_msg,
325-
log_count,
326-
//log_count == 0 ? " infinite" : "",
327-
cron_msg
328-
);
329-
if (time_next)
336+
if (is_crontab_trigger_msg)
330337
{
331-
localtime_s(&t1, &time_next);
332-
len = strftime(buffer + idx, ARRAYSIZE(buffer), " %Y-%m-%d %H:%M:%S ", &t1);
333-
idx += len;
338+
printf_to_bufferA(buffer, buffer_len - idx, idx,
339+
"[%s] [%s] [left count: %d] [%s]",
340+
name,
341+
log_msg,
342+
log_count,
343+
//log_count == 0 ? " infinite" : "",
344+
cron_msg
345+
);
346+
if (time_next)
347+
{
348+
localtime_s(&t1, &time_next);
349+
len = strftime(buffer + idx, ARRAYSIZE(buffer), " %Y-%m-%d %H:%M:%S ", &t1);
350+
idx += len;
351+
}
352+
}
353+
else
354+
{
355+
printf_to_bufferA(buffer, buffer_len - idx, idx,
356+
"[%s] [%s] [%s]",
357+
name,
358+
log_msg,
359+
cron_msg
360+
);
334361
}
335362

336-
std::string crontab_log_filename = crontab_config_ref["log"];
363+
std::string crontab_log_filename = jsp_crontab_config["log"];
337364
std::wstring crontab_log_filename_w = utf8_to_wstring(crontab_log_filename);
338365
if (TRUE == PathFileExists(crontab_log_filename_w.c_str()) && FileSize(crontab_log_filename_w.c_str()) > 1024 * 1024 * 10)
339366
{

CommandTrayHost/utils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bool try_read_optional_json(const nlohmann::json& root, Type& out, PCSTR query_s
4949

5050
//void from_json(const nlohmann::json& j, cron_expr& p);
5151

52-
void crontab_log(const nlohmann::json& jsp, time_t, time_t, PCSTR, PCSTR, int);
52+
void crontab_log(const nlohmann::json& jsp_crontab_config, time_t, time_t, PCSTR, PCSTR, PCSTR, int, int);
5353

5454
cron_expr* get_cron_expr(const nlohmann::json& jsp, cron_expr& result);
5555

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ example configure
8282
"count": 0, // times to run, 0 infinite
8383
// Optional
8484
"enabled": true,
85-
"log": "commandtrayhost.log",
85+
"log": "commandtrayhost.log", // comment out this line to disable logging
8686
"log_level": 0, // log level 0 1 2
8787
},
8888
},

README.zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Windows命令行程序系统托盘管理工具
8080
"enabled": true
8181
// 可选
8282
"crontab_config": { // crontab配置
83-
"crontab": "8 */2 15-16 29 2 *", // crontab语法具体参考上面8
83+
"crontab": "8 */2 15-16 29 2 *", // crontab语法具体参考Linux
8484
"method": "start", // 支持的有 start restart stop
8585
"count": 0, // 0 表示不只限制,大于0的整数,表示运行多少次就不运行了
8686
// 可选

0 commit comments

Comments
 (0)