Re-sync with internal repository
[hiphop-php.git] / third-party / watchman / src / watchman / cmds / log.cpp
blobccb849be974510d86381b765700b7405b0813176
1 /*
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.
6 */
8 #include "watchman/Client.h"
9 #include "watchman/Logging.h"
10 #include "watchman/watchman_cmd.h"
12 using namespace watchman;
14 // log-level "debug"
15 // log-level "error"
16 // log-level "off"
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;
23 try {
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();
33 switch (level) {
34 case watchman::OFF:
35 client->debugSub.reset();
36 client->errorSub.reset();
37 break;
38 case watchman::DBG:
39 client->debugSub = log.subscribe(watchman::DBG, notify);
40 client->errorSub = log.subscribe(watchman::ERR, notify);
41 break;
42 case watchman::ERR:
43 default:
44 client->debugSub.reset();
45 client->errorSub = log.subscribe(watchman::ERR, notify);
48 UntypedResponse resp;
49 resp.set("log_level", json_ref(args.at(1)));
50 return resp;
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;
61 try {
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");
71 UntypedResponse resp;
72 resp.set("logged", json_true());
73 return resp;
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;
84 try {
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);
92 UntypedResponse resp;
93 resp.set("log_level", json_ref(args.at(1)));
94 return resp;
96 W_CMD_REG(
97 "global-log-level",
98 cmd_global_log_level,
99 CMD_DAEMON | CMD_ALLOW_ANY_USER,
100 nullptr);
102 /* vim:ts=2:sw=2:et: