static BOOL check_head_valid( void ) { fs_handle_type file; file = open_log_file(); if( file == FS_NULL_HANDLE ) { return FALSE; } // 로그크기(LOG_SIZE) 동일한지 검사 if( get_value( file, LOG_SIZE_POS, 6 ) != LOG_SIZE ) { goto _fail; } // 로그최대개수(MAX_LOG) 일치하는지 검사 if( get_value( file, MAX_POS, 4 ) != MAX_LOG ) { goto _fail; } pantech_fclose( file ); return TRUE; _fail: pantech_fclose( file ); pantech_fremove( LOG_PATH ); return FALSE; }
static BOOL load_head( LOG_HEADER_T* head ) { BOOL create = FALSE; fs_handle_type file; sys_mkdir(LOG_DIR, 0700);//make directory.. if((file = pantech_fopen( LOG_PATH, O_RDONLY, S_IRUSR | S_IWUSR)) != NULL) { if( check_head_valid() == FALSE ) { create = TRUE; } pantech_fclose(file); } else { create = TRUE; } if( create == TRUE ) { if( create_log_file() == FALSE ) { return FALSE; } } file = open_log_file(); if( file == FS_NULL_HANDLE ) { return FALSE; } head->first_log_pos_ = get_value( file, FIRST_LOG_POS, 6 ); // FIRST LOG head->count_ = get_value( file, LOG_COUNT_POS, 4 ); // 로그수 head->head_ = get_value( file, HEAD_POS, 4 ); // head head->tail_ = get_value( file, TAIL_POS, 4 ); // tail head->free_head_ = get_value( file, FREE_HEAD_POS, 4 ); // free head if( head->first_log_pos_ == (UINT32)-1 || head->count_ == (UINT32)-1 || head->head_ == (UINT32)-1 || head->tail_ == (UINT32)-1 || head->free_head_ == (UINT32)-1 ) { goto _fail; } if( get_prev_next( file, head ) == FALSE ) { goto _fail; } pantech_fclose( file ); return TRUE; _fail: pantech_fclose( file ); return FALSE; }
int pantech_logfile_save_log( const char* msg ) { fs_handle_type file; file = open_log_file(); if( file == FS_NULL_HANDLE ) { return -1; } if( g_head.count_ < MAX_LOG ) { IDX_T new_idx; new_idx = add_log( file, &g_head, msg ); if( new_idx == NULL_IDX ) return FALSE; if( set_value( file, LOG_COUNT_POS, g_head.count_, 4 ) == FALSE ) return FALSE; if( set_value( file, TAIL_POS, g_head.tail_, 4 ) == FALSE ) return FALSE; if( set_value( file, FREE_HEAD_POS, g_head.free_head_, 4 ) == FALSE ) return FALSE; if( set_prev_next( file, &g_head, new_idx ) == FALSE ) return FALSE; if( g_head.count_ == 1 ) { if( set_value( file, HEAD_POS, g_head.head_, 4 ) == FALSE ) return FALSE; } else { if( set_prev_next( file, &g_head, g_head.prev_[ new_idx ] ) == FALSE ) return FALSE; } } else { IDX_T idx; idx = g_head.head_; g_head.head_ = g_head.next_[ idx ]; g_head.free_head_ = idx; g_head.next_[ idx ] = NULL_IDX; g_head.count_--; if( add_log( file, &g_head, msg ) != idx ) return FALSE; if( set_value( file, HEAD_POS, g_head.head_, 4 ) == FALSE ) return FALSE; if( set_value( file, TAIL_POS, g_head.tail_, 4 ) == FALSE ) return FALSE; if( set_prev_next( file, &g_head, idx ) == FALSE ) return FALSE; if( set_prev_next( file, &g_head, g_head.prev_[ idx ] ) == FALSE ) return FALSE; } pantech_fclose( file ); return 0; }
static BOOL get_log( IDX_T idx, char* log_buf ) { fs_handle_type file; UINT32 pos; char* iter; int res; file = open_log_file(); pos = g_head.first_log_pos_ + (UINT32)idx * ( 82 + 46 + 82 + LOG_SIZE + 2 + 2 ); pantech_fseek( file, SEEK_SET, pos ); res = pantech_fread( file, log_buf, LOG_SIZE ); if( res != LOG_SIZE ) { pantech_fclose( file ); return FALSE; } for( iter = log_buf + LOG_SIZE - 2; *iter == ' '; --iter ) {} iter[ 1 ] = '\0'; pantech_fclose( file ); return TRUE; }
static BOOL create_log_file( void ) { fs_handle_type file; char buf[ 128 ]; UINT32 size; int row, col, count; int a; UINT32 first_log_pos = 0; if ((file = pantech_fopen( LOG_PATH, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR)) == NULL) { printk("Cant open file\n"); return false; } pantech_fwrite( file, (char*)TITLE_LINE, sizeof( TITLE_LINE ) - 1); pantech_fwrite( file, (char*)TITLE_MSG, sizeof( TITLE_MSG ) - 1); pantech_fwrite( file, (char*)TITLE_SUB_LINE, sizeof( TITLE_SUB_LINE ) - 1); sprintf( buf, " - 로그크기 : 0x%04X\r\n", (int)LOG_SIZE ); size = strlen( buf ); pantech_fwrite( file, buf, size); sprintf( buf, " - MAX : 0x%02X\r\n", (int)MAX_LOG ); size = strlen( buf ); pantech_fwrite( file, buf, size); sprintf( buf, " - FIRST LOG : 0x0000\r\n" ); size = strlen( buf ); pantech_fwrite( file, buf, size); sprintf( buf, " - 로그수 : 0x00\r\n" ); size = strlen( buf ); pantech_fwrite( file, buf, size); sprintf( buf, " - head : 0xFF\r\n" ); size = strlen( buf ); pantech_fwrite( file, buf, size); sprintf( buf, " - tail : 0xFF\r\n" ); size = strlen( buf ); pantech_fwrite( file, buf, size); sprintf( buf, " - free head : 0x00\r\n" ); size = strlen( buf ); pantech_fwrite( file, buf, size ); buf[ 0 ] = ' '; buf[ 1 ] = 'F'; buf[ 2 ] = 'F'; for( row = 0, count = 0; TRUE; ++row ) { char* str; if( row == 0 ) { // 123456789012345 str = " - prev/next :"; } else { // 123456789012345 str = " "; } pantech_fwrite( file, str, 15); for( col = 0; col < 12; ++col ) { if( ++count == MAX_LOG ) { pantech_fwrite( file, " FFFF", 5); } else { sprintf( buf + 3, "%02X", count ); pantech_fwrite( file, buf, 5); } if( count == MAX_LOG ) { pantech_fwrite( file, "\r\n", 2); goto _loop_end; } if( col == 11 ) { pantech_fwrite( file, "\r\n", 2); } } } _loop_end: pantech_fwrite( file, (char*)TITLE_LINE, sizeof( TITLE_LINE ) - 1); pantech_fwrite( file, "\r\n", 2); for( a = 0; a < MAX_LOG; ++a ) { sprintf( buf, " 로그 %03d\r\n", a + 1 ); pantech_fwrite( file, "\r\n", 2 ); pantech_fwrite( file, (char*)TITLE_SUB_LINE, sizeof( TITLE_SUB_LINE ) - 1); pantech_fwrite( file, buf, strlen( buf )); pantech_fwrite( file, (char*)TITLE_SUB_LINE, sizeof( TITLE_SUB_LINE ) - 1); if( a == 0 ) { first_log_pos = pantech_ftell(file); } pantech_fwrite( file, g_blank_buf, LOG_SIZE + 2); } sprintf( buf, "%04X", (int)first_log_pos ); pantech_fseek( file, SEEK_SET, FIRST_LOG_POS + 2 ); pantech_fwrite( file, buf, 4); pantech_fclose( file ); return TRUE; }