Shad Ansari | 2f7f9be | 2017-06-07 13:34:53 -0700 | [diff] [blame^] | 1 | /* |
| 2 | <:copyright-BRCM:2016:DUAL/GPL:standard |
| 3 | |
| 4 | Broadcom Proprietary and Confidential.(c) 2016 Broadcom |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Unless you and Broadcom execute a separate written software license |
| 8 | agreement governing use of this software, this software is licensed |
| 9 | to you under the terms of the GNU General Public License version 2 |
| 10 | (the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php, |
| 11 | with the following added to such license: |
| 12 | |
| 13 | As a special exception, the copyright holders of this software give |
| 14 | you permission to link this software with independent modules, and |
| 15 | to copy and distribute the resulting executable under terms of your |
| 16 | choice, provided that you also meet, for each linked independent |
| 17 | module, the terms and conditions of the license of that module. |
| 18 | An independent module is a module which is not derived from this |
| 19 | software. The special exception does not apply to any modifications |
| 20 | of the software. |
| 21 | |
| 22 | Not withstanding the above, under no circumstances may you combine |
| 23 | this software in any way with any other Broadcom software provided |
| 24 | under a license other than the GPL, without Broadcom's express prior |
| 25 | written consent. |
| 26 | |
| 27 | :> |
| 28 | */ |
| 29 | |
| 30 | #include <bcmos_system.h> |
| 31 | #include <bcmcli.h> |
| 32 | #include <bcmolt_api.h> |
| 33 | #include <bcmolt_model_types.h> |
| 34 | #include "bcmolt_user_appl_remote_logger_cli.h" |
| 35 | #include "bcmolt_user_appl_remote_logger.h" |
| 36 | #include "bcmolt_conv.h" |
| 37 | |
| 38 | #define BCMOLT_REMOTE_LOGGER_DEFAULT_POLLING_PERIOD_MS 5000 |
| 39 | #define BCMOLT_REMOTE_LOGGER_DEFAULT_SUBSEQUENT_DELAY_MS 5 |
| 40 | #define BCMOLT_REMOTE_LOGGER_DEFAULT_MAX_FILE_SIZE (200 * 1000000) |
| 41 | #define BCMOLT_REMOTE_LOGGER_DEFAULT_FILE_REACHED_BEHAVIOR "stop" |
| 42 | #define BCMOLT_REMOTE_LOGGER_DEFAULT_FILENAME "remote_log.txt" |
| 43 | |
| 44 | static bcmos_errno remote_logger_configure_log( |
| 45 | bcmolt_devid device, |
| 46 | const bcmolt_log_entry_key *key, |
| 47 | bcmolt_log_level log_level_save) |
| 48 | { |
| 49 | bcmolt_log_entry_cfg cfg; |
| 50 | BCMOLT_CFG_INIT(&cfg, log_entry, *key); |
| 51 | BCMOLT_CFG_PROP_SET(&cfg, log_entry, log_level_save, log_level_save); |
| 52 | BCMOLT_CFG_PROP_SET(&cfg, log_entry, log_level_print, BCMOLT_LOG_LEVEL_WARNING); |
| 53 | return bcmolt_cfg_set(device, &cfg.hdr); |
| 54 | } |
| 55 | |
| 56 | static bcmos_errno remote_logger_configure_logs(bcmolt_devid device) |
| 57 | { |
| 58 | bcmos_errno err; |
| 59 | bcmolt_msg_set *msg_set; |
| 60 | bcmolt_log_entry_cfg multi_cfg; |
| 61 | bcmolt_log_entry_key multi_key = { .log_id = 0 }; /* start from the first log ID */ |
| 62 | uint16_t i; |
| 63 | |
| 64 | /* allocate space for multi-get return */ |
| 65 | err = bcmolt_msg_set_alloc(BCMOLT_OBJ_ID_LOG_ENTRY, BCMOLT_MGT_GROUP_CFG, 50, &msg_set); |
| 66 | if (err != BCM_ERR_OK) |
| 67 | { |
| 68 | return err; |
| 69 | } |
| 70 | |
| 71 | /* initialize the multi-get config structure */ |
| 72 | BCMOLT_CFG_INIT(&multi_cfg, log_entry, multi_key); |
| 73 | BCMOLT_MSGSET_CFG_PROP_GET(msg_set, log_entry, log_level_save); |
| 74 | |
| 75 | do |
| 76 | { |
| 77 | /* call multi-get API */ |
| 78 | err = bcmolt_cfg_get_multi(device, &multi_cfg.hdr, BCMOLT_FILTER_FLAGS_NONE, msg_set); |
| 79 | if (err != BCM_ERR_OK) |
| 80 | { |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | /* for each log entry, reconfigure it to print only warning level and above to the screen (keeping current |
| 85 | * behavior for printing to RAM files) */ |
| 86 | for (i = 0; i < msg_set->num_instances; i++) |
| 87 | { |
| 88 | bcmolt_log_entry_cfg *cfg = (bcmolt_log_entry_cfg *)msg_set->msg[i]; |
| 89 | err = remote_logger_configure_log(device, &cfg->key, cfg->data.log_level_save); |
| 90 | if (err != BCM_ERR_OK) |
| 91 | { |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* update the key for next call */ |
| 97 | multi_cfg.key = *((bcmolt_log_entry_key *)msg_set->next_key); |
| 98 | |
| 99 | /* keep calling the function until we have retrieved all entries */ |
| 100 | } while (msg_set->more); |
| 101 | |
| 102 | bcmolt_msg_set_free(msg_set); |
| 103 | return err; |
| 104 | } |
| 105 | |
| 106 | static bcmos_errno remote_logger_cmd_start(bcmcli_session *session, const bcmcli_cmd_parm parms[], uint16_t n_parms) |
| 107 | { |
| 108 | bcmos_errno err; |
| 109 | |
| 110 | /* these parameters will always be non-NULL since they have default values */ |
| 111 | bcmolt_remote_logger_cfg cfg = |
| 112 | { |
| 113 | .max_file_size = bcmcli_find_named_parm(session, "max_file_size")->value.unumber, |
| 114 | .max_file_size_reached_behavior = (bcmolt_remote_logger_file_size_reached_behavior) |
| 115 | bcmcli_find_named_parm(session, "file_size_reached")->value.number, |
| 116 | .polling_period_ms = bcmcli_find_named_parm(session, "polling_period")->value.unumber, |
| 117 | .subsequent_delay_ms = bcmcli_find_named_parm(session, "subsequent_delay")->value.unumber, |
| 118 | }; |
| 119 | strncpy( |
| 120 | cfg.filename, |
| 121 | bcmcli_find_named_parm(session, "filename")->value.string, |
| 122 | BCMOLT_REMOTE_LOGGER_FILENAME_MAX_LEN - 1); /* leave room for terminator */ |
| 123 | |
| 124 | /* if the user requested, set all logs on the device to print warnings/errors only */ |
| 125 | if ((bcmos_bool)bcmcli_find_named_parm(session, "configure_logs")->value.number) |
| 126 | { |
| 127 | err = remote_logger_configure_logs(current_device); |
| 128 | if (err != BCM_ERR_OK) |
| 129 | { |
| 130 | return err; |
| 131 | } |
| 132 | bcmcli_session_print(session, "Reconfigured all log entries to print warning/error messages only\n"); |
| 133 | } |
| 134 | |
| 135 | err = bcmolt_remote_logger_appl_cfg_update(current_device, &cfg); |
| 136 | if (err != BCM_ERR_OK) |
| 137 | { |
| 138 | return err; |
| 139 | } |
| 140 | |
| 141 | err = bcmolt_remote_logger_appl_start(current_device); |
| 142 | if (err == BCM_ERR_OK) |
| 143 | { |
| 144 | bcmcli_session_print(session, "Remote logger application started\n"); |
| 145 | } |
| 146 | return err; |
| 147 | } |
| 148 | |
| 149 | static bcmos_errno remote_logger_cmd_stop(bcmcli_session *session, const bcmcli_cmd_parm parms[], uint16_t n_parms) |
| 150 | { |
| 151 | bcmos_errno err = bcmolt_remote_logger_appl_stop(current_device); |
| 152 | if (err == BCM_ERR_OK) |
| 153 | { |
| 154 | bcmcli_session_print(session, "Remote logger application stopped\n"); |
| 155 | } |
| 156 | return err; |
| 157 | } |
| 158 | |
| 159 | static bcmos_errno remote_logger_cmd_status(bcmcli_session *session, const bcmcli_cmd_parm parms[], uint16_t n_parms) |
| 160 | { |
| 161 | static int2str_t file_size_reached2str[] = |
| 162 | { |
| 163 | { BCMOLT_REMOTE_LOGGER_FILE_SIZE_REACHED_BEHAVIOR_STOP, "stop" }, |
| 164 | { BCMOLT_REMOTE_LOGGER_FILE_SIZE_REACHED_BEHAVIOR_CLEAR, "clear" }, |
| 165 | { -1 } |
| 166 | }; |
| 167 | |
| 168 | bcmos_errno err; |
| 169 | bcmolt_remote_logger_cfg cfg; |
| 170 | |
| 171 | if (bcmolt_remote_logger_appl_is_running(current_device)) |
| 172 | { |
| 173 | bcmcli_session_print(session, "Remote logger application is running\n"); |
| 174 | err = bcmolt_remote_logger_appl_cfg_get(current_device, &cfg); |
| 175 | if (err != BCM_ERR_OK) |
| 176 | { |
| 177 | return err; |
| 178 | } |
| 179 | |
| 180 | bcmcli_session_print(session, "filename=%s\n", cfg.filename); |
| 181 | bcmcli_session_print(session, "max_file_size=%u\n", cfg.max_file_size); |
| 182 | bcmcli_session_print( |
| 183 | session, |
| 184 | "file_size_reached=%s\n", |
| 185 | int2str(file_size_reached2str, cfg.max_file_size_reached_behavior)); |
| 186 | bcmcli_session_print(session, "polling_period=%u\n", cfg.polling_period_ms); |
| 187 | bcmcli_session_print(session, "subsequent_delay=%u\n", cfg.subsequent_delay_ms); |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | bcmcli_session_print(session, "Remote logger application is not running\n"); |
| 192 | } |
| 193 | |
| 194 | return BCM_ERR_OK; |
| 195 | } |
| 196 | |
| 197 | bcmos_errno bcmolt_remote_logger_appl_cli_init(bcmcli_entry *top_dir) |
| 198 | { |
| 199 | static bcmcli_enum_val file_size_reached_behavior_table[] = |
| 200 | { |
| 201 | { "stop", BCMOLT_REMOTE_LOGGER_FILE_SIZE_REACHED_BEHAVIOR_STOP }, |
| 202 | { "clear", BCMOLT_REMOTE_LOGGER_FILE_SIZE_REACHED_BEHAVIOR_CLEAR }, |
| 203 | BCMCLI_ENUM_LAST |
| 204 | }; |
| 205 | |
| 206 | bcmcli_entry *dir = bcmcli_dir_add( |
| 207 | top_dir, |
| 208 | "remote_logger", |
| 209 | "Periodically copy device log to local file", |
| 210 | BCMCLI_ACCESS_ADMIN, |
| 211 | NULL); |
| 212 | BCMOS_CHECK_RETURN_ERROR(!dir, BCM_ERR_NOMEM); |
| 213 | |
| 214 | BCMCLI_MAKE_CMD(dir, "start", "Start periodically copying device log to host", remote_logger_cmd_start, |
| 215 | BCMCLI_MAKE_PARM_DEFVAL( |
| 216 | "filename", |
| 217 | "Output file path", |
| 218 | BCMCLI_PARM_STRING, |
| 219 | BCMCLI_PARM_FLAG_OPTIONAL, |
| 220 | (long)BCMOLT_REMOTE_LOGGER_DEFAULT_FILENAME), |
| 221 | BCMCLI_MAKE_PARM_DEFVAL( |
| 222 | "max_file_size", |
| 223 | "Maximum output file size in bytes", |
| 224 | BCMCLI_PARM_UNUMBER, |
| 225 | BCMCLI_PARM_FLAG_OPTIONAL, |
| 226 | BCMOLT_REMOTE_LOGGER_DEFAULT_MAX_FILE_SIZE), |
| 227 | BCMCLI_MAKE_PARM_ENUM_DEFVAL( |
| 228 | "file_size_reached", |
| 229 | "Behavior when max file size is reached", |
| 230 | file_size_reached_behavior_table, |
| 231 | BCMCLI_PARM_FLAG_OPTIONAL, |
| 232 | BCMOLT_REMOTE_LOGGER_DEFAULT_FILE_REACHED_BEHAVIOR), |
| 233 | BCMCLI_MAKE_PARM_DEFVAL( |
| 234 | "polling_period", |
| 235 | "Polling period in ms", |
| 236 | BCMCLI_PARM_UNUMBER, |
| 237 | BCMCLI_PARM_FLAG_OPTIONAL, |
| 238 | BCMOLT_REMOTE_LOGGER_DEFAULT_POLLING_PERIOD_MS), |
| 239 | BCMCLI_MAKE_PARM_DEFVAL( |
| 240 | "subsequent_delay", |
| 241 | "Delay between repeated API calls in ms", |
| 242 | BCMCLI_PARM_UNUMBER, |
| 243 | BCMCLI_PARM_FLAG_OPTIONAL, |
| 244 | BCMOLT_REMOTE_LOGGER_DEFAULT_SUBSEQUENT_DELAY_MS), |
| 245 | BCMCLI_MAKE_PARM_ENUM_DEFVAL( |
| 246 | "configure_logs", |
| 247 | "Configure all device logs to print error/warning only", |
| 248 | bcmcli_enum_bool_table, |
| 249 | BCMCLI_PARM_FLAG_OPTIONAL, |
| 250 | "yes")); |
| 251 | |
| 252 | BCMCLI_MAKE_CMD_NOPARM(dir, "stop", "Stop periodically copying device log to host", remote_logger_cmd_stop); |
| 253 | |
| 254 | BCMCLI_MAKE_CMD_NOPARM(dir, "status", "Show status / configuration parameters", remote_logger_cmd_status); |
| 255 | |
| 256 | return BCM_ERR_OK; |
| 257 | } |