#include "stdafx.h" #include "Log_IO.h" // Headers for additional types #include "Vect3usi.h" // use %v using namespace std; // Private variables and their access functions volatile BOOL locked = FALSE; FILE *log_file_prv_ptr; char log_file_name_prv[MAX_FILE_STR_LEN]; char log_num_buf_prv[50]; char log_time_formatted_prv[12]; char log_out_txt_prv[MAX_LOG_ENTRY_LENGTH]; bool log_flush_after_write_prv = true; // ---------------------------------------------------------------------------------------- // get_log_file_name // // Description - Returns name of currently used log file // ---------------------------------------------------------------------------------------- char* get_log_file_name() { return log_file_name_prv; } // ---------------------------------------------------------------------------------------- // disable_log_flush // // Description - Prevents immidiate writing to file, improving performance but potentially // delays information visibility. (Flush is enabled by default) // ---------------------------------------------------------------------------------------- void disable_log_flush() { log_flush_after_write_prv = false; } // ---------------------------------------------------------------------------------------- // enable_log_flush // // Description - Enables immidiate writing to file, reducing performance but providing // instant log entries. (Flush is enabled by default) // ---------------------------------------------------------------------------------------- void enable_log_flush() { log_flush_after_write_prv = true; } // ======================================================================================// // ---------------------------------------------------------------------------------------- // write_to_log // // Description - Similar to printf(), but writes to the current log file with a time stamp. // // Possible format options: // %s - for string // %d - for 32 bit or less numericals (ie: int, short, unsigned short, char, unsigned char) // %f - for float or double // %v - Vect3usi object pointer // %% - for '%' // ---------------------------------------------------------------------------------------- void write_to_log(const char *fmt, ...) { // Make sure the log file exists if ( log_file_prv_ptr == NULL ) { AfxMessageBox( _T("Warning! Attempted to write to non-existant Log File.") ); return; } va_list args; int cur_input_indx=0, cur_out_indx=0; time_t tp; struct tm *detailed_time; unsigned int year, month, day, hour, minute, second; // Make sure only one thread is writing to the log at a time while( locked ); locked = TRUE; // Clear the current buffer memset(log_out_txt_prv, 0, MAX_LOG_ENTRY_LENGTH); // Initialize args va_start(args, fmt); //if (fmt != "**DATE**") if ( strcmp(fmt, "**DATE**") ) //ie !=0 { // Go through input string, extracting arguments or until end while ( (fmt[cur_input_indx] != 0) && (cur_out_indx < MAX_LOG_ENTRY_LENGTH) ) { if ( fmt[cur_input_indx] == '%' ) { char *append = 0; // Advance input index pointer ++cur_input_indx; switch ( fmt[cur_input_indx] ) { case '%': log_out_txt_prv[cur_out_indx] = '%'; ++cur_out_indx; break; // For strings case 's': append = va_arg(args, char*); break; // For 32 bit or less numericals (ie: int, short, unsigned short, char, unsigned char) case 'd': { int num = va_arg(args, int); sprintf(log_num_buf_prv, "%d", num); append = log_num_buf_prv; } break; // For float or double case 'f': { double num = va_arg(args, double); sprintf(log_num_buf_prv, "%f", num); append = log_num_buf_prv; } break; // For Vect3usi case 'v': { Vect3usi *vect_ptr = va_arg(args, Vect3usi*); sprintf(log_num_buf_prv, "(x%d,y%d,z%d)", vect_ptr->x, vect_ptr->y, vect_ptr->z); append = log_num_buf_prv; } break; // Add any additional type actions here default: va_arg( args, void* ); sprintf( log_num_buf_prv, "UNKNOWN_TYPE" ); append = log_num_buf_prv; } cur_out_indx += (int)strlen(append); if ( (cur_out_indx < MAX_LOG_ENTRY_LENGTH) && (append) ) { strcat(log_out_txt_prv, append); } } else { log_out_txt_prv[cur_out_indx] = fmt[cur_input_indx]; ++cur_out_indx; } ++cur_input_indx; } } // Close argument list va_end(args); time(&tp); detailed_time = localtime(&tp); year = detailed_time->tm_year - 100; month = detailed_time->tm_mon;// + 1; day = detailed_time->tm_mday; hour = detailed_time->tm_hour; minute = detailed_time->tm_min; second = detailed_time->tm_sec; char log_time_formatted_prv[14]; char date_string[32]; //if (fmt == "**DATE**") if ( strcmp(fmt, "**DATE**") == 0 ) { // Year if (year < 10) sprintf(date_string, "200%d-", year); else sprintf(date_string, "20%d-", year); // Month if (month < 10) sprintf(date_string,"%s0%d-",date_string, month+1); else sprintf(date_string,"%s%d-",date_string, month+1); // Day if (day < 10) sprintf(date_string,"%s0%d",date_string, day); else sprintf(date_string,"%s%d",date_string, day); } { // Format the time to look pretty if (hour < 10) sprintf(log_time_formatted_prv,"0%d:",hour); else sprintf(log_time_formatted_prv,"%d:",hour); if (minute < 10) sprintf(log_time_formatted_prv,"%s0%d:",log_time_formatted_prv, minute); else sprintf(log_time_formatted_prv,"%s%d:",log_time_formatted_prv, minute); if (second < 10) sprintf(log_time_formatted_prv,"%s0%d",log_time_formatted_prv, second); else sprintf(log_time_formatted_prv,"%s%d",log_time_formatted_prv, second); } //if (fmt == "**DATE**") if ( strcmp(fmt, "**DATE**") == 0 ) { // Write to log file "time DATE" fprintf( log_file_prv_ptr, "%s \t %s\n", log_time_formatted_prv, date_string ); } else { // Write to log file "time message" fprintf( log_file_prv_ptr, "%s \t %s\n", log_time_formatted_prv, log_out_txt_prv ); } if ( log_flush_after_write_prv ) { fflush( log_file_prv_ptr ); } locked = FALSE; } // ---------------------------------------------------------------------------------------- // write_to_log_empty_lines // // Description - Just writes blank lines to the log (visual purposes) // ---------------------------------------------------------------------------------------- void write_to_log_empty_lines( int number_of_lines ) { // Make sure only one thread is writing to the log at a time while( locked ); locked = TRUE; for ( int i = 0; i < number_of_lines; i++ ) { fprintf( log_file_prv_ptr, "\n" ); } // No need to flush, next important data will for us locked = FALSE; } // ---------------------------------------------------------------------------------------- // init_log_file // // Description - Simply closes the current log file // ---------------------------------------------------------------------------------------- void close_log_file( ) { fclose( log_file_prv_ptr ); log_file_prv_ptr = NULL; } // ---------------------------------------------------------------------------------------- // init_log_file // // Description - Ones a log file under subdirectory "PoroMedia_Logs", with the name // 'log_name' + (Year-Month-Day-Hour-Minute) stamp. // ---------------------------------------------------------------------------------------- int init_log_file( char* log_name ) { time_t tp; struct tm *detailed_time; int year, month, day, hour, minute, second; time(&tp); detailed_time = localtime(&tp); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // int tm_sec; // seconds after the minute // int tm_min; // minutes after the hour // int tm_hour; // hours since midnight // int tm_mday; // day of the month // int tm_mon; // months since January // int tm_year; // years since 1900 // int tm_wday; // days since Sunday // int tm_yday; // days since January 1 // int tm_isdst; // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ year = detailed_time->tm_year - 100; month = detailed_time->tm_mon;// + 1; //twice +1 ? (see // Month below) day = detailed_time->tm_mday; hour = detailed_time->tm_hour; minute = detailed_time->tm_min; second = detailed_time->tm_sec; CreateDirectory( _T("PoroMedia_Logs"), NULL ); char log_time_formatted_prv[14]; // Year if (year < 10) sprintf(log_time_formatted_prv, "200%d-", year); else sprintf(log_time_formatted_prv, "20%d-", year); // Month if (month < 10) sprintf(log_time_formatted_prv,"%s0%d-",log_time_formatted_prv, month+1); else sprintf(log_time_formatted_prv,"%s%d-",log_time_formatted_prv, month+1); // Day if (day < 10) sprintf(log_time_formatted_prv,"%s0%d",log_time_formatted_prv, day); else sprintf(log_time_formatted_prv,"%s%d",log_time_formatted_prv, day); // Hour if (hour < 10) sprintf(log_time_formatted_prv,"%s_0%d", log_time_formatted_prv, hour); else sprintf(log_time_formatted_prv,"%s_%d", log_time_formatted_prv, hour); // Minute if (minute < 10) sprintf(log_time_formatted_prv,"%s0%d",log_time_formatted_prv, minute); else sprintf(log_time_formatted_prv,"%s%d",log_time_formatted_prv, minute); // Create final name, and open the log file sprintf( log_file_name_prv, "PoroMedia_Logs\\%s_%s.txt", log_name, log_time_formatted_prv); log_file_prv_ptr = fopen( log_file_name_prv, "w" ); if( !log_file_prv_ptr ) { AfxMessageBox( _T("Error! Could not create a log file.") ); return 1; } fprintf( log_file_prv_ptr, "PoroMedia Run Log\n" ); fprintf( log_file_prv_ptr, "Started on %d-%d-%d, at %d:%d:%d\n\n", 2000+year, month+1, day, hour, minute, second ); return 0; } // // // ///* */ // LARGE_INTEGER ticksPerSecond; // LARGE_INTEGER tick; // A point in time // LARGE_INTEGER tick_end; // A point in time // // LARGE_INTEGER time; // For converting tick into real time // // // get the high resolution counter's accuracy // QueryPerformanceFrequency(&ticksPerSecond); // // // what time is it? // QueryPerformanceCounter(&tick); // // // DSinfo* temp; // // for (register unsigned short int i = 0; i < 1000; i++){ // temp = create_DS(100,1000,1000); // free_DS(temp); // } // // QueryPerformanceCounter(&tick_end); // // // convert the tick number into the number of seconds // // since the system was started... // /*time.QuadPart*/ // double time = (tick_end.QuadPart - tick.QuadPart)/ticksPerSecond.QuadPart; // char log_txt[MAX_FILE_STR_LEN]; // sprintf(log_txt, "%f = %lld - %lld / %lld", time, tick_end.QuadPart, tick.QuadPart, ticksPerSecond.QuadPart); // write_to_log(log_txt); // // exit(0); // //// result += AnsiString(time.QuadPart); //// //// get the number of hours //// int hours = time.QuadPart/3600; //// //// get the number of minutes //// time.QuadPart = time.QuadPart - (hours * 3600); //// int minutes = time.QuadPart/60; //// //// get the number of seconds //// int seconds = time.QuadPart - (minutes * 60); //// //// AnsiString result = "The system was started "; //// result += IntToStr(hours) + " hours "; //// result += IntToStr(minutes) + " minutes "; //// //result += IntToStr(seconds) + " and "; //// result += AnsiString(time.QuadPart); //// // % ticksPerSecond.QuadPart); //// // result += AnsiString("/") + ticksPerSecond.QuadPart; ////// result += " seconds ago."; //// char log_txt[MAX_FILE_STR_LEN]; //// sprintf(log_txt, "%f = %ld - %ld / %ld", double, tick_end); //// write_to_log(log_txt); //// //// exit(0); /////////////////////////////////////////////////////////////