1 /*************************************************************************
3 * Copyright 2020 highstreet technologies GmbH and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 ***************************************************************************/
20 #include "log_utils.h"
28 #include <core/framework.h>
30 static int instances = 0;
31 static int errors = 0;
32 static FILE* logfile = 0;
34 static char *extract_format(const char *format);
36 void log_init(const char *logfilename) {
37 assert(instances == 0);
40 logfile = fopen(logfilename, "w");
44 time_t t = time(NULL);
45 struct tm tm = *localtime(&t);
46 fprintf(logfile, "started at: %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
47 fprintf(stdout, LOG_COLOR_BOLD_RED"started at: %d-%02d-%02d %02d:%02d:%02d\n"LOG_COLOR_RESET, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
50 void log__message(char const * const filename, uint32_t location, const int verbose_level, const char *format, ...) {
54 asprintf(&format2, "[%s:%u] %s", filename, location, format);
56 fprintf(stderr, LOG_COLOR_BOLD_RED"bad malloc in log system\n"LOG_COLOR_RESET);
57 format2 = (char *)format;
60 va_start(arg, format);
61 char *new_format = extract_format(format2);
62 vfprintf(logfile, new_format, arg);
64 if(new_format != format2) {
68 if(format2 != format) {
74 if(verbose_level <= framework_arguments.verbosity_level) {
75 va_start(arg, format);
76 vfprintf(stdout, format, arg);
81 void log__error(char const * const function, uint32_t location, const char *format, ...) {
83 bool has_newline = false;
84 if(format[strlen(format) - 1] == '\n') {
89 char *new_format = extract_format(format);
90 fprintf(logfile, "[error in %s():%d] ", function, location);
91 va_start(arg, format);
92 vfprintf(logfile, new_format, arg);
93 if(new_format != format) {
97 fprintf(logfile, "\n");
102 fprintf(stderr, "["LOG_COLOR_RED"error in "LOG_COLOR_BOLD_RED"%s()"LOG_COLOR_RED":"LOG_COLOR_BOLD_CYAN"%d"LOG_COLOR_RESET"] ", function, location);
103 va_start(arg, format);
104 vfprintf(stderr, format, arg);
106 fprintf(stderr, "\n");
111 void log_close(void) {
112 time_t t = time(NULL);
113 struct tm tm = *localtime(&t);
114 fprintf(logfile, "finished at: %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
118 fprintf(stderr, "-------- !!!!!! ERRORS WERE PRESENT, CHECK ERROR FILE !!!!!! ------------\n\n\n\n");
122 static char *extract_format(const char *format) {
125 int l = strlen(format);
126 char *ret = (char *)malloc(sizeof(char) * (l + 1));
128 fprintf(stderr, LOG_COLOR_BOLD_RED"bad malloc in log system\n"LOG_COLOR_RESET);
129 return (char *)format;
134 bool in_escape = false;
139 if(format[s] == 27) {
145 if(format[s] == 'm') {
153 ret[d++] = format[s];