1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2009 Michael Gernoth <michael at gernoth.net>
3 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
8 //-----------------------------------------------------------------------------
10 //-----------------------------------------------------------------------------
13 #ifndef EXTERNAL_PRINTANDLOG
17 #include <readline/readline.h>
24 double CursorScaleFactor
= 1;
25 int PlotGridX
=0, PlotGridY
=0, PlotGridXdefault
= 64, PlotGridYdefault
= 64, CursorCPos
= 0, CursorDPos
= 0;
26 bool flushAfterWrite
= false; //buzzy
28 bool GridLocked
= false;
29 bool showDemod
= true;
31 static char *logfilename
= "proxmark3.log";
33 #ifndef EXTERNAL_PRINTANDLOG
34 static pthread_mutex_t print_lock
= PTHREAD_MUTEX_INITIALIZER
;
36 void PrintAndLogEx(logLevel_t level
, char *fmt
, ...) {
38 // skip debug messages if client debugging is turned off i.e. 'DATA SETDEBUG 0'
39 // if (g_debugMode == 0 && level == DEBUG)
42 char buffer
[MAX_PRINT_BUFFER
] = {0};
43 char buffer2
[MAX_PRINT_BUFFER
] = {0};
44 char prefix
[20] = {0};
47 // {NORMAL, SUCCESS, INFO, FAILED, WARNING, ERR, DEBUG}
48 static char *prefixes
[7] = { "", "", "INFO: ", "FAILED: ", "WARNING: ", "ERROR: ", "#: "};
52 strncpy(prefix
,_RED_(FAILED
: ), sizeof(prefix
)-1);
55 strncpy(prefix
,_BLUE_(#: ), sizeof(prefix)-1);
58 strncpy(prefix
,_GREEN_( ), sizeof(prefix
)-1);
61 strncpy(prefix
,_CYAN_(WARNING
: ), sizeof(prefix
)-1);
64 strncpy(prefix
, prefixes
[level
], sizeof(prefix
)-1);
70 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
73 // no prefixes for normal
74 if ( level
== NORMAL
) {
79 if (strchr(buffer
, '\n')) {
81 const char delim
[2] = "\n";
83 // line starts with newline
84 if (buffer
[0] == '\n')
87 token
= strtok(buffer
, delim
);
89 while (token
!= NULL
) {
91 size
= strlen(buffer2
);
94 snprintf(buffer2
+size
, sizeof(buffer2
)-size
, "%s%s\n", prefix
, token
);
96 snprintf(buffer2
+size
, sizeof(buffer2
)-size
, "\n");
98 token
= strtok(NULL
, delim
);
100 PrintAndLog(buffer2
);
102 snprintf(buffer2
, sizeof(buffer2
), "%s%s", prefix
, buffer
);
103 PrintAndLog(buffer2
);
107 void PrintAndLog(char *fmt
, ...)
111 va_list argptr
, argptr2
;
112 static FILE *logfile
= NULL
;
113 static int logging
=1;
115 // lock this section to avoid interlacing prints from different threads
116 pthread_mutex_lock(&print_lock
);
118 if (logging
&& !logfile
) {
119 logfile
=fopen(logfilename
, "a");
121 fprintf(stderr
, "Can't open logfile, logging disabled!\n");
126 // If there is an incoming message from the hardware (eg: lf hid read) in
127 // the background (while the prompt is displayed and accepting user input),
128 // stash the prompt and bring it back later.
129 #ifdef RL_STATE_READCMD
130 // We are using GNU readline. libedit (OSX) doesn't support this flag.
131 int need_hack
= (rl_readline_state
& RL_STATE_READCMD
) > 0;
134 saved_point
= rl_point
;
135 saved_line
= rl_copy_text(0, rl_end
);
137 rl_replace_line("", 0);
142 va_start(argptr
, fmt
);
143 va_copy(argptr2
, argptr
);
144 vprintf(fmt
, argptr
);
145 printf(" "); // cleaning prompt
149 #ifdef RL_STATE_READCMD
150 // We are using GNU readline. libedit (OSX) doesn't support this flag.
153 rl_replace_line(saved_line
, 0);
154 rl_point
= saved_point
;
160 if (logging
&& logfile
) {
161 vfprintf(logfile
, fmt
, argptr2
);
162 fprintf(logfile
,"\n");
167 if (flushAfterWrite
) //buzzy
172 pthread_mutex_unlock(&print_lock
);
176 void SetLogFilename(char *fn
)
181 void SetFlushAfterWrite(bool flush_after_write
) {
182 flushAfterWrite
= flush_after_write
;