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