From 828ffdabb4be784971342bb987df15a5810a99c1 Mon Sep 17 00:00:00 2001
From: Markus Hitter <mah@jump-ing.de>
Date: Sat, 16 Nov 2013 23:37:37 +0100
Subject: Use avr_message() for verbose messages.

This enhances encapsulation and simplifies code, both of which are
generally considered to be a good thing.
---
 src/avrdevice.cpp |  3 +--
 src/avrerror.cpp  |  3 +++
 src/cmd/main.cpp  | 49 ++++++++++++++++---------------------------------
 src/hwstack.cpp   |  3 +--
 4 files changed, 21 insertions(+), 37 deletions(-)

diff --git a/src/avrdevice.cpp b/src/avrdevice.cpp
index 37b236d..aff75a7 100644
--- a/src/avrdevice.cpp
+++ b/src/avrdevice.cpp
@@ -275,8 +275,7 @@ int AvrDevice::Step(bool &untilCoreStepFinished, SystemClockOffset *nextStepIn_n
             }
 
             if(EP.end() != find(EP.begin(), EP.end(), PC)) {
-                if(global_verbose_on)
-                    cout << "Simulation finished!" << endl;
+                avr_message("Simulation finished!");
                 SystemClock::Instance().stop();
                 dump_manager->cycle();
                 return 0;
diff --git a/src/avrerror.cpp b/src/avrerror.cpp
index a67d656..4156160 100644
--- a/src/avrerror.cpp
+++ b/src/avrerror.cpp
@@ -128,6 +128,9 @@ void SystemConsoleHandler::TraceNextLine(void) {
 }
 
 void SystemConsoleHandler::vfmessage(const char *fmt, ...) {
+    if ( ! global_verbose_on)
+        return;
+
     va_list ap;
     snprintf(formatStringBuffer, sizeof(formatStringBuffer),
              "MESSAGE %s", fmt);
diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp
index 71c2fb3..539ea3f 100644
--- a/src/cmd/main.cpp
+++ b/src/cmd/main.cpp
@@ -255,37 +255,30 @@ int main(int argc, char *argv[]) {
                     cerr << "maxRunTime is zero" << endl;
                     exit(1);
                 }
-                if(global_verbose_on)
-                    cout << "Maximum Run Time: " << maxRunTime << endl;
+                avr_message("Maximum Run Time: %lld", maxRunTime);
                 break;
             
             case 'u':
-                if(global_verbose_on)
-                    cout << "Run with User Interface at Port 7777" << endl;
+                avr_message("Run with User Interface at Port 7777");
                 userinterface_flag = 1;
                 break;
             
             case 'f':
-                if(global_verbose_on)
-                    cout << "File to load: " << optarg << endl;
+                avr_message("File to load: %s", optarg);
                 filename = optarg;
                 break;
             
             case 'd':
-                if(global_verbose_on)
-                    cout << "Device to simulate: " << optarg << endl;
                 devicename = optarg;
                 break;
             
             case 'g':
-                if(global_verbose_on)
-                    cout << "Running as gdb-server" << endl;
+                avr_message("Running as gdb-server");
                 gdbserver_flag = 1;
                 break;
             
             case 'G':
-                if(global_verbose_on)
-                    cout << "Running with debug information from gdbserver" << endl;
+                avr_message("Running with debug information from gdbserver");
                 global_gdb_debug = 1;
                 gdbserver_flag = 1;
                 break;
@@ -295,13 +288,12 @@ int main(int argc, char *argv[]) {
                     cerr << "GDB Server Port is not a number" << endl;
                     exit(1);
                 }
-                if(global_verbose_on)
-                    cout << "Running on port: " << optarg << endl;
+                avr_message("Running on port: %ld", global_gdbserver_port);
                 break;
             
             case 't':
-                if(global_verbose_on)
-                    cout << "Running in Trace Mode with maximum " << linestotrace << " lines per file" << endl;
+                avr_message("Running in Trace Mode with maximum %lld lines per file",
+                            linestotrace);
 
                 sysConHandler.SetTraceFile(optarg, linestotrace);
                 break;
@@ -375,33 +367,26 @@ int main(int argc, char *argv[]) {
     
     //if we want to insert some special "pipe" Registers we could do this here:
     if(readFromPipeFileName != "") {
-        if(global_verbose_on)
-            cout << "Add ReadFromPipe-Register at 0x"
-                 << hex << readFromPipeOffset
-                 << " and read from file: " << readFromPipeFileName << endl;
+        avr_message("Add ReadFromPipe-Register at 0x%lx and read from file: %s",
+                    readFromPipeOffset, readFromPipeFileName.c_str());
         dev1->ReplaceIoRegister(readFromPipeOffset,
             new RWReadFromFile(dev1, "FREAD", readFromPipeFileName.c_str()));
     }
     
     if(writeToPipeFileName != "") {
-        if(global_verbose_on)
-            cout << "Add WriteToPipe-Register at 0x"
-                 << hex << writeToPipeOffset
-                 << " and write to file: " << writeToPipeFileName << endl;
+        avr_message("Add WriteToPipe-Register at 0x%lx and write to file: %s",
+                    writeToPipeOffset, writeToPipeFileName.c_str());
         dev1->ReplaceIoRegister(writeToPipeOffset,
             new RWWriteToFile(dev1, "FWRITE", writeToPipeFileName.c_str()));
     }
     
     if(writeToAbort) {
-        if(global_verbose_on)
-            cout << "Add WriteToAbort-Register at 0x" << hex
-                 << writeToAbort << endl;
+        avr_message("Add WriteToAbort-Register at 0x%lx", writeToAbort);
         dev1->ReplaceIoRegister(writeToAbort, new RWAbort(dev1, "ABORT"));
     }
     
     if(writeToExit) {
-        if(global_verbose_on)
-            cout << "Add WriteToExit-Register at 0x" << hex << writeToExit << endl;
+        avr_message("Add WriteToExit-Register at 0x%lx", writeToExit);
         dev1->ReplaceIoRegister(writeToExit, new RWExit(dev1, "EXIT"));
     }
     
@@ -413,8 +398,7 @@ int main(int argc, char *argv[]) {
     //if we have a file we can check out for termination lines.
     vector<string>::iterator ii;
     for(ii = terminationArgs.begin(); ii != terminationArgs.end(); ii++) {
-        if(global_verbose_on)
-            cout << "Termination or Breakpoint Symbol: " << *ii << endl;
+        avr_message("Termination or Breakpoint Symbol: %s", (*ii).c_str());
         dev1->RegisterTerminationSymbol((*ii).c_str());
     }
     
@@ -445,8 +429,7 @@ int main(int argc, char *argv[]) {
             SystemClock::Instance().Run(maxRunTime);
         }
     } else { // gdb should be activated
-        if(global_verbose_on)
-            cout << "Going to gdb..." << endl;
+        avr_message("Going to gdb...");
         GdbServer gdb1(dev1, global_gdbserver_port, global_gdb_debug, globalWaitForGdbConnection);
         SystemClock::Instance().Add(&gdb1);
         SystemClock::Instance().Endless();
diff --git a/src/hwstack.cpp b/src/hwstack.cpp
index c28bf17..e08b990 100644
--- a/src/hwstack.cpp
+++ b/src/hwstack.cpp
@@ -322,8 +322,7 @@ void ThreadList::OnPop()
 	new_thread->m_ip = 0x0000;
 	new_thread->m_alive = true;
 
-    if(global_verbose_on)
-        fprintf(stderr, "Context switch at PC 0x%05x from thread %d to %d\n", addr, m_cur_thread, n);
+    avr_message("Context switch at PC 0x%05x from thread %d to %d\n", addr, m_cur_thread, n);
     m_cur_thread = n;
 }
 
-- 
1.8.3.2

