klog.h
Go to the documentation of this file.
1 /*
2  * KubOS Core Flight Services
3  * Copyright (C) 2015 Kubos Corporation
4  *
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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  */
17 
33 #ifndef KLOG_H
34 #define KLOG_H
35 
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <csp/csp_types.h>
39 
40 //#define MODULE_LOG
41 #include "kubos-core/k_log.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 #ifndef KLOG_MAX_LINE
48 #define KLOG_MAX_LINE 255
49 #endif
50 
51 #define KLOG(handle, level, logger, ...) klog_write(handle, level, logger, __VA_ARGS__)
52 #define KLOG_ERR(handle, logger, ...) KLOG(handle, LOG_ERROR, logger, __VA_ARGS__)
53 #define KLOG_WARN(handle, logger, ...) KLOG(handle, LOG_WARNING, logger, __VA_ARGS__)
54 #define KLOG_TELEMETRY(handle, logger, ...) KLOG(handle, LOG_TELEMETRY, logger, __VA_ARGS__)
55 #define KLOG_INFO(handle, logger, ...) KLOG(handle, LOG_INFO, logger, __VA_ARGS__)
56 #define KLOG_DEBUG(handle, logger, ...) KLOG(handle, LOG_DEBUG, logger, __VA_ARGS__)
57 
58 
59 #define KLOG_SUFFIX_LEN 4
60 #define KLOG_PATH_LEN 255
61 #define KLOG_MAX_PATH (KLOG_PATH_LEN - KLOG_SUFFIX_LEN - 1)
62 
63 #define KLOG_PART_SIZE_DEFAULT (1024 * 512)
64 #define KLOG_MAX_PARTS_DEFAULT 4
65 
66 typedef struct
67 {
68  char *file_path;
69  uint8_t file_path_len;
70  uint32_t part_size;
71  uint8_t max_parts;
73  uint8_t klog_file_level;
75 } klog_config;
76 
77 typedef struct
78 {
79  FILE *log_file;
80  uint8_t current_part;
83 } klog_handle;
84 
85 int klog_init_file(klog_handle *handle);
86 void klog_console(unsigned level, const char *logger, const char *format, ...);
87 void klog_file(klog_handle *handle, unsigned level, const char *logger, const char *format, ...);
88 void klog_cleanup(klog_handle *handle);
89 
90 #define klog_write(handle, level, logger, ...) do { \
91  if (level <= ((handle)->config.klog_console_level)) { \
92  klog_console(level, logger, __VA_ARGS__); \
93  } \
94  if (level <= ((handle)->config.klog_file_level) && ((handle)->config.klog_file_logging)) { \
95  klog_file(handle, level, logger, __VA_ARGS__); \
96  } \
97 } while (0)
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif
104 
105 /* @} */
System logging header.
Definition: klog.h:77
uint32_t part_size
Definition: klog.h:70
uint8_t max_parts
Definition: klog.h:71
char * file_path
Definition: klog.h:68
uint8_t klog_file_level
Definition: klog.h:73
FILE * log_file
Definition: klog.h:79
void klog_console(unsigned level, const char *logger, const char *format,...)
uint8_t klog_console_level
Definition: klog.h:72
uint8_t file_path_len
Definition: klog.h:69
void klog_cleanup(klog_handle *handle)
Definition: klog.h:66
bool klog_file_logging
Definition: klog.h:74
klog_config config
Definition: klog.h:82
void klog_file(klog_handle *handle, unsigned level, const char *logger, const char *format,...)
uint32_t current_part_size
Definition: klog.h:81
int klog_init_file(klog_handle *handle)
uint8_t current_part
Definition: klog.h:80