| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- #include <stdio.h>
- #include <stdarg.h>
- #include <stdint.h>
- #include "logger.h"
- #include "app/protocol.h"
- static uint32_t level_data[2];
- static int _get_level(int mod){
- int index = mod_bit_start(mod)/(32+1);
- mod = mod_bit_start(mod) % 32;
- return (level_data[index] >> mod) & LEVEL_MASK;
- }
- void set_log_level(int mod, int l){
- int index = mod_bit_start(mod)/(32+1);
- mod = mod_bit_start(mod) % 32;
- level_data[index] = (level_data[index] & (~(LEVEL_MASK<<mod))) | ((l & LEVEL_MASK)<<mod);
- }
- void set_log_all(int l){
- for (int i = 0; i < 32; i++){
- set_log_level(i, l);
- }
- }
- static void log_out(char *fmt, va_list args){
- vprintf(fmt, args);
- }
- void log_debug(int mod, char *fmt, ...){
- if (_get_level(mod) >= L_debug){
- va_list args;
- va_start(args, fmt);
- log_out(fmt, args);
- va_end(args);
- }
- }
- void log_warning(int mod, char *fmt, ...){
- if (_get_level(mod) >= L_warning){
- va_list args;
- va_start(args, fmt);
- log_out(fmt, args);
- va_end(args);
- }
- }
- void log_error(int mod, char *fmt, ...){
- if (_get_level(mod) >= L_error){
- va_list args;
- va_start(args, fmt);
- log_out(fmt, args);
- va_end(args);
- }
- }
- //rewrite the fputc, so that the printf,vprintf can log the info the can
- static uint8_t log_buffer[64];
- static int log_index = 0;
- int fputc(int c, FILE *fp){
- if (c == '\n'){ //don't send '\n'
- if (log_index != 0){
- protocol_send_debug_info(0x72, log_buffer, log_index);
- }
- log_index = 0;
- }else if (log_index == 64){
- protocol_send_debug_info(0x70 ,log_buffer, log_index);
- log_index = 0;
- log_buffer[log_index++] = c;
- }else{
- log_buffer[log_index++] = c;
- }
-
- return 1;
- }
|