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