|
| 1 | +// Copyright (c) 2012-2019 The Bitcoin developers |
| 2 | +// Copyright (c) 2019 The Gridcoin developers. |
| 3 | +// Distributed under the MIT/X11 software license, see the accompanying |
| 4 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | + |
| 6 | +#include "rpcprotocol.h" |
| 7 | +#include "util.h" |
| 8 | + |
| 9 | +#include <univalue.h> |
| 10 | + |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +static void EnableOrDisableLogCategories(UniValue cats, bool enable) { |
| 14 | + |
| 15 | + std::vector<std::string> vcats; |
| 16 | + |
| 17 | + // The below is to allow non-array parameters. The current front-end for Gridcoin does not |
| 18 | + // understand JSON parameters. This will handle that when it does, and deal with single |
| 19 | + // parameters too. |
| 20 | + if (cats.isArray()) |
| 21 | + { |
| 22 | + for (unsigned int i = 0; i < cats.size(); ++i) |
| 23 | + { |
| 24 | + vcats.push_back(cats[i].get_str()); |
| 25 | + } |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + vcats.push_back(cats.get_str()); |
| 30 | + } |
| 31 | + |
| 32 | + for (const auto& cat : vcats) |
| 33 | + { |
| 34 | + bool success; |
| 35 | + if (enable) { |
| 36 | + LogPrintf("INFO: EnableOrDisableLogCategories: enabling category %s", cat); |
| 37 | + success = LogInstance().EnableCategory(cat); |
| 38 | + } else { |
| 39 | + LogPrintf("INFO: EnableOrDisableLogCategories: disabling category %s", cat); |
| 40 | + success = LogInstance().DisableCategory(cat); |
| 41 | + } |
| 42 | + |
| 43 | + if (!success) { |
| 44 | + throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +UniValue logging(const UniValue& params, bool fHelp) |
| 50 | +{ |
| 51 | + if (fHelp || params.size() > 2) |
| 52 | + { |
| 53 | + throw runtime_error( |
| 54 | + "logging [json array category adds] [json array category removes]\n" |
| 55 | + "Gets and sets the logging configuration.\n" |
| 56 | + "When called without an argument, returns the list of categories with status that are currently being debug logged or not.\n" |
| 57 | + "When called with arguments, adds or removes categories from debug logging and return the lists above.\n" |
| 58 | + "The arguments are evaluated in order \"include\", \"exclude\".\n" |
| 59 | + "If an item is both included and excluded, it will thus end up being excluded.\n" |
| 60 | + "The valid logging categories are: " + ListLogCategories() + "\n" |
| 61 | + "In addition, the following are available as category names with special meanings:\n" |
| 62 | + "all or 1: represent all logging categories.\n" |
| 63 | + "none or 0: even if other logging categories are specified, ignore all of them.\n\n" |
| 64 | + "Examples:\n" |
| 65 | + "logging all net: enables all and disables net.\n" |
| 66 | + "logging none: disables all.\n\n" |
| 67 | + "Note that unlike Bitcoin, we don't process JSON arrays correctly as arguments yet for the command line,\n" |
| 68 | + "so, for the rpc cli, it is limited to one enable and/or one disable category.\n" |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + if (params.size() >= 1) EnableOrDisableLogCategories(params[0], true); |
| 73 | + |
| 74 | + if (params.size() == 2) EnableOrDisableLogCategories(params[1], false); |
| 75 | + |
| 76 | + UniValue result(UniValue::VOBJ); |
| 77 | + std::vector<CLogCategoryActive> vLogCatActive = ListActiveLogCategories(); |
| 78 | + for (const auto& logCatActive : vLogCatActive) { |
| 79 | + result.pushKV(logCatActive.category, logCatActive.active); |
| 80 | + } |
| 81 | + |
| 82 | + return result; |
| 83 | +} |
| 84 | + |
0 commit comments