00001 00004 /* 00005 * ============================================================================== 00006 * Name : redirectprintf.c 00007 * Part of : Open C/Example code 00008 * Description : Contained the implementation of how to redirect stdout into a file. 00009 * Version : 1.0 00010 * 00011 * Copyright (c) 2007 Nokia Corporation. 00012 * This material, including documentation and any related 00013 * computer programs, is protected by copyright controlled by 00014 * Nokia Corporation. 00015 * ============================================================================== 00016 */ 00017 00018 // system include 00019 #include <stdio.h> 00020 // GCCE specific header file 00021 //#include <staticlibinit_gcce.h> 00022 00023 00024 // Debug message 00025 FILE* fpDebug = NULL; 00026 char* logFileName = "c:\\logs\\trace.txt"; 00027 const char* anotherLogFile = "c:\\logs\\newlog.txt"; 00028 00035 int RedirectPrintf(char* aMessagePtr, ...) 00036 { 00037 va_list marker; 00038 int printChar; 00039 if(fpDebug == NULL) 00040 { 00041 fpDebug = freopen(logFileName, "a+", stdout); 00042 } 00043 if(fpDebug != NULL) 00044 { 00045 va_start(marker, aMessagePtr); 00046 printChar = vprintf(aMessagePtr, marker); 00047 va_end(marker); 00048 } 00049 return printChar; 00050 } 00051 00056 int main(void) 00057 { 00058 int printInt = 10; 00059 // this will print in console 00060 printf("Redirecting printf to - %s\n", logFileName); 00061 // from this statement onwards, printf statement will be redirected to file 00062 printf("You wnt be able to see the message within the \nconsole.\n"); 00063 // put this message first. Once RedirectPrintf statement is called, printf statement will be redirect to file. 00064 printf("Press enter to exit."); 00065 RedirectPrintf("Printing something using trace.\n"); 00066 if(fpDebug != NULL) 00067 { 00068 printf("printing using printf method.\n"); 00069 printf("Printing an integer - %d.\n", printInt); 00070 00071 printf("Redirecting printf to another file - %s\n", anotherLogFile); 00072 fpDebug = freopen(anotherLogFile, "a+", fpDebug); 00073 if(fpDebug != NULL) 00074 { 00075 printf("Start redirecting printf to - %s\n", anotherLogFile); 00076 printf("Closing the printf stream.\n"); 00077 fclose(fpDebug); 00078 } 00079 } 00080 fflush(stdin); 00081 getchar(); // wait for user input before terminate 00082 return 0; 00083 } 00084 00085 00086 //End of file
Copyright ©2010 Nokia Corporation and/or its subsidiary(-ies).
All rights
reserved. Unless otherwise stated, these materials are provided under the terms of the Eclipse Public License
v1.0.