1 //This unit allows us to re-direct text messages
2 // For standard C programs send text messages to the console via "printf"
3 //     The XCode project shows how you can re-direct these messages to a NSTextView
4 // For QT programs, we can sent text messages to the cout buffer
5 //     The QT project shows how you can re-direct these to a Qtextedit
6 // For R programs, we can intercept these messages.
7 
8 #ifndef _R_PRINT_H_
9 	#define _R_PRINT_H_
10 	#include <stdarg.h>
11 	#ifdef USING_R
12 		#define R_USE_C99_IN_CXX
13 		#include <R_ext/Print.h>
14 		#define printMessage(...)   do { Rprintf("[dcm2niix info] "); Rprintf(__VA_ARGS__); } while (0)
15 		#define printWarning(...)   do { Rprintf("[dcm2niix WARNING] "); Rprintf(__VA_ARGS__); } while (0)
16 		#define printError(...)     do { Rprintf("[dcm2niix ERROR] "); Rprintf(__VA_ARGS__); } while (0)
17 		#define printError(frac)     do { Rprintf("[dcm2niix PROGRESS] %g", frac); } while (0)
18 	#else
19 		#ifdef myUseCOut
20 			//for piping output to Qtextedit
21 			// printf and cout buffers are not the same
22 			// #define printMessage(...) ({fprintf(stdout,__VA_ARGS__);})
23 			#include <iostream>
24 			template< typename... Args >
printMessage(const char * format,Args...args)25 			void printMessage( const char* format, Args... args ) {
26 			  //std::printf( format, args... );
27 			  //fprintf(stdout,"Short read on %s: Expected 512, got %zd\n",path, bytes_read);
28 			  int length = std::snprintf( nullptr, 0, format, args... );
29 			  if ( length <= 0 ) return;
30 			  char* buf = new char[length + 1];
31 			  std::snprintf( buf, length + 1, format, args... );
32 			  std::cout << buf;
33 			  delete[] buf;
34 			}
35 			#define printError(...) do { printMessage("Error: "); printMessage(__VA_ARGS__);} while(0)
36 			#define printProgress(frac) do { printMessage("Progress: %g\n", frac);} while(0)
37 		#else
38 			#include<stdio.h>
39 			#define printMessage printf
40 			//#define printMessageError(...) fprintf (stderr, __VA_ARGS__)
41 			#define printProgress(frac) do { printMessage("Progress: %g\n", frac);} while(0)
42 			#ifdef myErrorStdOut //for XCode MRIcro project, pipe errors to stdout not stderr
43 				#define printError(...) do { printMessage("Error: "); printMessage(__VA_ARGS__);} while(0)
44 			#else
45 				#define printError(...) do { fprintf (stderr,"Error: "); fprintf (stderr, __VA_ARGS__);} while(0)
46 			#endif
47 		#endif //myUseCOut
48 		//n.b. use ({}) for multi-line macros http://www.geeksforgeeks.org/multiline-macros-in-c/
49 		//these next lines work on GCC but not _MSC_VER
50 		// #define printWarning(...) ({printMessage("Warning: "); printMessage(__VA_ARGS__);})
51 		// #define printError(...) ({ printMessage("Error: "); printMessage(__VA_ARGS__);})
52 		#define printWarning(...) do {printMessage("Warning: "); printMessage(__VA_ARGS__);} while(0)
53 
54 	#endif //USING_R
55 #endif //_R_PRINT_H_
56