[669f5cae] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup logset
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file Change logger behavior.
|
---|
| 33 | */
|
---|
| 34 | #include <stdio.h>
|
---|
[69c664e] | 35 | #include <stdlib.h>
|
---|
[669f5cae] | 36 | #include <async.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <str_error.h>
|
---|
| 39 | #include <io/logctl.h>
|
---|
| 40 |
|
---|
[494f417] | 41 | static log_level_t parse_log_level_or_die(const char *log_level)
|
---|
[669f5cae] | 42 | {
|
---|
[494f417] | 43 | log_level_t result;
|
---|
[b7fd2a0] | 44 | errno_t rc = log_level_from_str(log_level, &result);
|
---|
[eab3d04] | 45 | if (rc != EOK) {
|
---|
| 46 | fprintf(stderr, "Unrecognised log level '%s': %s.\n",
|
---|
[494f417] | 47 | log_level, str_error(rc));
|
---|
| 48 | exit(2);
|
---|
[eab3d04] | 49 | }
|
---|
[494f417] | 50 | return result;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | static void usage(const char *progname)
|
---|
| 54 | {
|
---|
| 55 | fprintf(stderr, "Usage:\n");
|
---|
| 56 | fprintf(stderr, " %s <default-logging-level>\n", progname);
|
---|
[4b640a2] | 57 | fprintf(stderr, " %s <log-name> <logging-level>\n", progname);
|
---|
[494f417] | 58 | }
|
---|
[669f5cae] | 59 |
|
---|
[494f417] | 60 | int main(int argc, char *argv[])
|
---|
| 61 | {
|
---|
| 62 | if (argc == 2) {
|
---|
| 63 | log_level_t new_default_level = parse_log_level_or_die(argv[1]);
|
---|
[b7fd2a0] | 64 | errno_t rc = logctl_set_default_level(new_default_level);
|
---|
[669f5cae] | 65 |
|
---|
[494f417] | 66 | if (rc != EOK) {
|
---|
| 67 | fprintf(stderr, "Failed to change default logging level: %s.\n",
|
---|
| 68 | str_error(rc));
|
---|
| 69 | return 2;
|
---|
| 70 | }
|
---|
| 71 | } else if (argc == 3) {
|
---|
| 72 | log_level_t new_level = parse_log_level_or_die(argv[2]);
|
---|
[f039dba] | 73 | const char *logname = argv[1];
|
---|
[b7fd2a0] | 74 | errno_t rc = logctl_set_log_level(logname, new_level);
|
---|
[494f417] | 75 |
|
---|
| 76 | if (rc != EOK) {
|
---|
| 77 | fprintf(stderr, "Failed to change logging level: %s.\n",
|
---|
| 78 | str_error(rc));
|
---|
| 79 | return 2;
|
---|
| 80 | }
|
---|
| 81 | } else {
|
---|
| 82 | usage(argv[0]);
|
---|
| 83 | return 1;
|
---|
[669f5cae] | 84 | }
|
---|
| 85 |
|
---|
| 86 | return 0;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /** @}
|
---|
| 90 | */
|
---|