[9761] | 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 |
|
---|
[11199] | 11 | int PrintfFunction(const char* format,...){
|
---|
[9761] | 12 | /*http://linux.die.net/man/3/vsnprintf*/
|
---|
| 13 |
|
---|
| 14 | /*string to be printed: */
|
---|
| 15 | char *buffer = NULL;
|
---|
| 16 | int n,size = 100;
|
---|
[13612] | 17 | int my_rank;
|
---|
[9761] | 18 | //variable list of arguments
|
---|
| 19 | va_list args;
|
---|
[13622] | 20 |
|
---|
[13612] | 21 | /*recover my_rank:*/
|
---|
| 22 | my_rank=IssmComm::GetRank();
|
---|
[9761] | 23 |
|
---|
| 24 | while(true){
|
---|
| 25 |
|
---|
| 26 | /*allocate buffer for given string size*/
|
---|
[12428] | 27 | buffer=xNew<char>(size);
|
---|
[9761] | 28 |
|
---|
| 29 | /* Try to print in the allocated space. */
|
---|
| 30 | va_start(args, format);
|
---|
| 31 | n=vsnprintf(buffer,size,format,args);
|
---|
| 32 | va_end(args);
|
---|
| 33 |
|
---|
| 34 | /* If that worked, return the string. */
|
---|
| 35 | if(n>-1 && n<size) break;
|
---|
| 36 |
|
---|
| 37 | /* Else try again with more space. */
|
---|
| 38 | if(n>-1) /* glibc 2.1 */
|
---|
| 39 | size=n+1; /* precisely what is needed */
|
---|
| 40 | else /* glibc 2.0 */
|
---|
| 41 | size*=2; /* twice the old size */
|
---|
| 42 |
|
---|
[12428] | 43 | xDelete<char>(buffer);
|
---|
[9761] | 44 | }
|
---|
| 45 |
|
---|
| 46 | /*Ok, if we are running in parallel, get node 0 to print*/
|
---|
[13612] | 47 | if(my_rank==0)_printString_(buffer);
|
---|
[9761] | 48 |
|
---|
| 49 | /*Clean up and return*/
|
---|
[12428] | 50 | xDelete<char>(buffer);
|
---|
[9761] | 51 | return 1;
|
---|
| 52 | }
|
---|
[12512] | 53 | int PrintfFunction(const string & message){
|
---|
[13612] | 54 | int my_rank;
|
---|
[13622] | 55 |
|
---|
[13612] | 56 | /*recover my_rank:*/
|
---|
| 57 | my_rank=IssmComm::GetRank();
|
---|
[13609] | 58 |
|
---|
[13612] | 59 | if(my_rank==0){
|
---|
[12512] | 60 | printf("%s\n",message.c_str());
|
---|
| 61 | }
|
---|
[12543] | 62 | return 1;
|
---|
[12512] | 63 | }
|
---|
[12513] | 64 | int PrintfFunction2(const string & message){
|
---|
[13612] | 65 | int my_rank;
|
---|
[13609] | 66 |
|
---|
[13612] | 67 | /*recover my_rank:*/
|
---|
| 68 | my_rank=IssmComm::GetRank();
|
---|
[13609] | 69 |
|
---|
[13612] | 70 | if(my_rank==0){
|
---|
[12513] | 71 | printf("%s",message.c_str());
|
---|
| 72 | }
|
---|
[12543] | 73 | return 1;
|
---|
[12513] | 74 | }
|
---|