diff options
Diffstat (limited to 'userspace/src/io.c')
-rw-r--r-- | userspace/src/io.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/userspace/src/io.c b/userspace/src/io.c new file mode 100644 index 00000000..21ac54da --- /dev/null +++ b/userspace/src/io.c | |||
@@ -0,0 +1,105 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | * copy of this software and associated documentation files (the "Software"), | ||
6 | * to deal in the Software without restriction, including without limitation | ||
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | * and/or sell copies of the Software, and to permit persons to whom the | ||
9 | * Software is furnished to do so, subject to the following conditions: | ||
10 | * | ||
11 | * The above copyright notice and this permission notice shall be included in | ||
12 | * all copies or substantial portions of the Software. | ||
13 | * | ||
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | * DEALINGS IN THE SOFTWARE. | ||
21 | */ | ||
22 | |||
23 | #include <stdio.h> | ||
24 | #include <stdarg.h> | ||
25 | #include <string.h> | ||
26 | |||
27 | #include <unit/io.h> | ||
28 | #include <unit/args.h> | ||
29 | #include <unit/core.h> | ||
30 | #include <unit/unit.h> | ||
31 | |||
32 | #define MAX_LOG_LINE_LENGTH 4096 | ||
33 | |||
34 | static void __core_print_file(struct unit_fw *fw, FILE *filp, | ||
35 | const char *prefix, const char *msg, | ||
36 | const char *color) | ||
37 | { | ||
38 | if (color == NULL || args(fw)->no_color) | ||
39 | color = ""; | ||
40 | |||
41 | fprintf(filp, "[%s%s%s] %s%s%s", | ||
42 | color, prefix, C_RESET, | ||
43 | color, msg, C_RESET); | ||
44 | } | ||
45 | |||
46 | __attribute__((format (printf, 3, 4))) | ||
47 | void __core_print_stdout(struct unit_fw *fw, const char *color, | ||
48 | const char *fmt, ...) | ||
49 | { | ||
50 | va_list args; | ||
51 | char buf[MAX_LOG_LINE_LENGTH]; | ||
52 | |||
53 | va_start(args, fmt); | ||
54 | vsnprintf(buf, MAX_LOG_LINE_LENGTH, fmt, args); | ||
55 | va_end(args); | ||
56 | |||
57 | buf[MAX_LOG_LINE_LENGTH - 1] = 0; | ||
58 | |||
59 | __core_print_file(fw, stdout, "C", buf, color); | ||
60 | } | ||
61 | |||
62 | __attribute__((format (printf, 2, 3))) | ||
63 | void __core_print_stderr(struct unit_fw *fw, const char *fmt, ...) | ||
64 | { | ||
65 | va_list args; | ||
66 | char buf[MAX_LOG_LINE_LENGTH]; | ||
67 | |||
68 | va_start(args, fmt); | ||
69 | vsnprintf(buf, MAX_LOG_LINE_LENGTH, fmt, args); | ||
70 | va_end(args); | ||
71 | |||
72 | buf[MAX_LOG_LINE_LENGTH - 1] = 0; | ||
73 | |||
74 | __core_print_file(fw, stdout, "E", buf, C_RED); | ||
75 | } | ||
76 | |||
77 | __attribute__((format (printf, 3, 4))) | ||
78 | void __unit_info_color(struct unit_module *unit, const char *color, | ||
79 | const char *fmt, ...) | ||
80 | { | ||
81 | va_list args; | ||
82 | char buf[MAX_LOG_LINE_LENGTH]; | ||
83 | char *msg_start; | ||
84 | int written; | ||
85 | |||
86 | /* | ||
87 | * Default color for module prints is blue. Users can still turn this | ||
88 | * off with '-C'. | ||
89 | */ | ||
90 | if (color == NULL) | ||
91 | color = C_BLUE; | ||
92 | |||
93 | /* | ||
94 | * First prepend the unit name to the print. | ||
95 | */ | ||
96 | written = snprintf(buf, MAX_LOG_LINE_LENGTH, " [%s] ", unit->name); | ||
97 | |||
98 | msg_start = buf + written; | ||
99 | |||
100 | va_start(args, fmt); | ||
101 | vsnprintf(msg_start, MAX_LOG_LINE_LENGTH - written, fmt, args); | ||
102 | va_end(args); | ||
103 | |||
104 | __core_print_file(unit->fw, stdout, "T", buf, color); | ||
105 | } | ||