To log data

From ThaiEasyElecWiki
Jump to: navigation, search

Our example code has provided task to log data into files on SD card. The code is in log_manager.c. The original example code has integrated logging task into main routine. They are in AppInit() and AppTask500ms() in app_bs_sun_demo.c.

void AppInit(void)
{
	...
	log_init(); //this must be placed after SD card initialized
	...
}

log_init checks the existing of the log file for that day and creates it if it's not found.

void AppTask500ms(void)
{
	if (time_500ms_cnt & 1)
	{	
		...
	}
	else
	{
		...
		log_task();
	}
}

log_task checks the date changes and creates new files.


The folder created to store log files is, by default, "log". But you can change this by defining it in app_config.h.


The format of log file names and folder names can be set using some "#define".

1. You can set to use year directory or not by define LOG_USE_YEAR_DIR as 1 or 0 respectively. And you its format by defining LOG_YEAR_DIR_NAME.

2. You can set to use month directory or not by define LOG_USE_MONTH_DIR as 1 or 0 respectively. And you can set its format by defining LOG_MONTH_DIR_NAME.

3. You can set log file names by defining LOG_FILE_NAME.

4. Any appearance of keywords below will be replaced according to the date at that time got from RTC.

-"yyyy" will be replaced with 4-digit year, for example, "2012".

-"yy" will be replaced with 2-digit year, for example, "12".

-"MMM" will be replaced with one of "JAN", "FEB", "MAR", ... , "DEC" (month names).

-"mm" will be replaced by 2-digit month, for example, "02".

-"dd" (one appearance is expected) will be replaced by date in month, for example, "31".


See examples below:

//Example, use subfolder for month and year
#define LOG_USE_YEAR_DIR	1
#define LOG_USE_MONTH_DIR	1
#define LOG_YEAR_DIR_NAME	"yyyy"
#define LOG_MONTH_DIR_NAME	"mm"
#define LOG_FILE_NAME		"dd.log"
//############################################

With these settings, you will get "2012/02/14.log" on 14 Feb 2012.


//Example, use subfolder for year only
#define LOG_USE_YEAR_DIR	1
#define LOG_USE_MONTH_DIR	0
#define LOG_YEAR_DIR_NAME	"yyyy"
#define LOG_FILE_NAME		"MMM-dd.log"
//############################################

With these settings, you will get "2012/FEB-14.log" on 14 Feb 2012.


//Example, use subfolder for month only
#define LOG_USE_YEAR_DIR	0
#define LOG_USE_MONTH_DIR	1
#define LOG_MONTH_DIR_NAME	"yy-mm"
#define LOG_FILE_NAME		"dd.log"
//############################################

With these settings, you will get "12-02/14.log" on 14 Feb 2012.


//Example, use no subfolder
#define LOG_USE_YEAR_DIR	0
#define LOG_USE_MONTH_DIR	0
#define LOG_FILE_NAME		"yy-mm-dd.log"
//############################################

With these settings, you will get "12-02-14.log" on 14 Feb 2012.


Once the log file of that time is created, you can log data with log_write.

void log_write(char *dat)

Note that it accepts texts, not binary data. And neither LF nor CR is added, it means that you have to write "\r\n" yourself.


Finally, you can set how long to keep log files on SD card by defining LOG_KEEP_DAY in app_config.h. By default, it's 60 days. The files (and folders) older than this will be deleted by log_task. But if you want to keep them forever, just define it 0.

Personal tools
Namespaces
Variants
Actions
Navigation
Articles and Tutorials
Shortcut (mini SUN7)
Toolbox