00001
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <stdio.h>
00020
00021
00022
00023
00024
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
00060 printf("Redirecting printf to - %s\n", logFileName);
00061
00062 printf("You wnt be able to see the message within the \nconsole.\n");
00063
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();
00082 return 0;
00083 }
00084
00085
00086