Nokia N97 SDK
Example Applications Guide

redirectprintf.c File Reference


Detailed Description

Contained the implementation of how to redirect stdout into a file.

Definition in file redirectprintf.c.

#include <stdio.h>

Go to the source code of this file.


Functions

int  RedirectPrintf (char *aMessagePtr,...)
int  main (void)

Variables

FILE *  fpDebug = NULL
char *  logFileName = "c:\\logs\\trace.txt"
const char *  anotherLogFile = "c:\\logs\\newlog.txt"


Function Documentation

int RedirectPrintf (  char *  aMessagePtr,
  ...
) 
 

Use to redirect the printf statements

Parameters:
aMessagePtr  pointer to the message
...  Variable parameters like printf

Definition at line 35 of file redirectprintf.c.

References fpDebug, and logFileName.

Referenced by main().

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 }

int main (  void   ) 
 

application entry point

Definition at line 56 of file redirectprintf.c.

References anotherLogFile, fpDebug, logFileName, and RedirectPrintf().

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 }


© Nokia 2009

Back to top