2 * Copyright (c) Meta Platforms, Inc. and affiliates.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
8 #include "watchman/Client.h"
9 #include "watchman/Logging.h"
10 #include "watchman/watchman_cmd.h"
12 using namespace watchman
;
17 static UntypedResponse
cmd_loglevel(Client
* client
, const json_ref
& args
) {
18 if (json_array_size(args
) != 2) {
19 throw ErrorResponse("wrong number of arguments to 'log-level'");
22 watchman::LogLevel level
;
24 level
= watchman::logLabelToLevel(json_to_w_string(args
.at(1)));
25 } catch (std::out_of_range
&) {
26 throw ErrorResponse("invalid log level for 'log-level'");
29 auto clientRef
= client
->shared_from_this();
30 auto notify
= [clientRef
]() { clientRef
->ping
->notify(); };
31 auto& log
= watchman::getLog();
35 client
->debugSub
.reset();
36 client
->errorSub
.reset();
39 client
->debugSub
= log
.subscribe(watchman::DBG
, notify
);
40 client
->errorSub
= log
.subscribe(watchman::ERR
, notify
);
44 client
->debugSub
.reset();
45 client
->errorSub
= log
.subscribe(watchman::ERR
, notify
);
49 resp
.set("log_level", json_ref(args
.at(1)));
52 W_CMD_REG("log-level", cmd_loglevel
, CMD_DAEMON
, NULL
);
54 // log "debug" "text to log"
55 static UntypedResponse
cmd_log(Client
*, const json_ref
& args
) {
56 if (json_array_size(args
) != 3) {
57 throw ErrorResponse("wrong number of arguments to 'log'");
60 watchman::LogLevel level
;
62 level
= watchman::logLabelToLevel(json_to_w_string(args
.at(1)));
63 } catch (std::out_of_range
&) {
64 throw ErrorResponse("invalid log level for 'log'");
67 auto text
= json_to_w_string(args
.at(2));
69 watchman::log(level
, text
, "\n");
72 resp
.set("logged", json_true());
75 W_CMD_REG("log", cmd_log
, CMD_DAEMON
| CMD_ALLOW_ANY_USER
, NULL
);
77 // change the server log level for the logs
78 static UntypedResponse
cmd_global_log_level(Client
*, const json_ref
& args
) {
79 if (json_array_size(args
) != 2) {
80 throw ErrorResponse("wrong number of arguments to 'global-log-level'");
83 watchman::LogLevel level
;
85 level
= watchman::logLabelToLevel(json_to_w_string(args
.at(1)));
86 } catch (std::out_of_range
&) {
87 throw ErrorResponse("invalid log level for 'global-log-level'");
90 watchman::getLog().setStdErrLoggingLevel(level
);
93 resp
.set("log_level", json_ref(args
.at(1)));
99 CMD_DAEMON
| CMD_ALLOW_ANY_USER
,