source: issm/trunk-jpl/src/c/io/PrintfFunction.cpp@ 13412

Last change on this file since 13412 was 13412, checked in by Mathieu Morlighem, 12 years ago

CHG: lots of cleanup

File size: 1.4 KB
Line 
1/*\file PrintfFunction.c
2 *\brief: this function is used by the _printf_ macro, to take into account the
3 *fact we may be running on a cluster.
4 */
5
6#include <stdarg.h>
7#include <stdio.h>
8#include "../shared/shared.h"
9#include "../include/include.h"
10
11int PrintfFunction(const char* format,...){
12 /*http://linux.die.net/man/3/vsnprintf*/
13
14 /*string to be printed: */
15 char *buffer = NULL;
16 int n,size = 100;
17 int string_size;
18 extern int my_rank;
19 //variable list of arguments
20 va_list args;
21
22 while(true){
23
24 /*allocate buffer for given string size*/
25 buffer=xNew<char>(size);
26
27 /* Try to print in the allocated space. */
28 va_start(args, format);
29 n=vsnprintf(buffer,size,format,args);
30 va_end(args);
31
32 /* If that worked, return the string. */
33 if(n>-1 && n<size) break;
34
35 /* Else try again with more space. */
36 if(n>-1) /* glibc 2.1 */
37 size=n+1; /* precisely what is needed */
38 else /* glibc 2.0 */
39 size*=2; /* twice the old size */
40
41 xDelete<char>(buffer);
42 }
43
44 /*Ok, if we are running in parallel, get node 0 to print*/
45 if(my_rank==0)_printString_(buffer);
46
47 /*Clean up and return*/
48 xDelete<char>(buffer);
49 return 1;
50}
51int PrintfFunction(const string & message){
52 extern int my_rank;
53 if(my_rank==0){
54 printf("%s\n",message.c_str());
55 }
56 return 1;
57}
58int PrintfFunction2(const string & message){
59 extern int my_rank;
60 if(my_rank==0){
61 printf("%s",message.c_str());
62 }
63 return 1;
64}
Note: See TracBrowser for help on using the repository browser.