| 132 | } |
| 133 | |
| 134 | static void log_to_files(const char *log_prefix, |
| 135 | const char *entry_prefix, |
| 136 | enum log_level level, |
| 137 | const struct node_id *node_id, |
| 138 | const struct timeabs *time, |
| 139 | const char *str, |
| 140 | const u8 *io, |
| 141 | size_t io_len, |
| 142 | bool print_timestamps, |
| 143 | FILE **outfiles) |
| 144 | { |
| 145 | char tstamp[sizeof("YYYY-mm-ddTHH:MM:SS.nnnZ ")]; |
| 146 | char *entry; |
| 147 | |
| 148 | if (print_timestamps) { |
| 149 | char iso8601_msec_fmt[sizeof("YYYY-mm-ddTHH:MM:SS.%03dZ ")]; |
| 150 | strftime(iso8601_msec_fmt, sizeof(iso8601_msec_fmt), "%FT%T.%%03dZ ", gmtime(&time->ts.tv_sec)); |
| 151 | snprintf(tstamp, sizeof(tstamp), iso8601_msec_fmt, (int) time->ts.tv_nsec / 1000000); |
| 152 | } else |
| 153 | tstamp[0] = '\0'; |
| 154 | |
| 155 | if (level == LOG_IO_IN || level == LOG_IO_OUT) { |
| 156 | const char *dir = level == LOG_IO_IN ? "[IN]" : "[OUT]"; |
| 157 | char *hex = tal_hexstr(NULL, io, io_len); |
| 158 | if (!node_id) |
| 159 | entry = tal_fmt(tmpctx, "%s%s%s: %s%s %s\n", |
| 160 | log_prefix, tstamp, entry_prefix, str, dir, hex); |
| 161 | else |
| 162 | entry = tal_fmt(tmpctx, "%s%s%s-%s: %s%s %s\n", |
| 163 | log_prefix, tstamp, |
| 164 | node_id_to_hexstr(tmpctx, node_id), |
| 165 | entry_prefix, str, dir, hex); |
| 166 | tal_free(hex); |
| 167 | } else { |
| 168 | if (!node_id) |
| 169 | entry = tal_fmt(tmpctx, "%s%s%s %s: %s\n", |
| 170 | log_prefix, tstamp, level_prefix(level), entry_prefix, str); |
| 171 | else |
| 172 | entry = tal_fmt(tmpctx, "%s%s%s %s-%s: %s\n", |
| 173 | log_prefix, tstamp, level_prefix(level), |
| 174 | node_id_to_hexstr(tmpctx, node_id), |
| 175 | entry_prefix, str); |
| 176 | } |
| 177 | |
| 178 | /* Default if nothing set is stdout */ |
| 179 | if (!outfiles) { |
| 180 | fwrite(entry, strlen(entry), 1, stdout); |
| 181 | fflush(stdout); |
| 182 | } |
| 183 | for (size_t i = 0; i < tal_count(outfiles); i++) { |
| 184 | fwrite(entry, strlen(entry), 1, outfiles[i]); |
| 185 | fflush(outfiles[i]); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | static size_t mem_used(const struct log_entry *e) |
| 190 | { |
no test coverage detected