kick
/Users/morten/Programmering/cpp/kick/src/kick/core/debug.cpp
00001 //
00002 // Created by morten on 15/07/14.
00003 //
00004 
00005 #ifdef _WIN32
00006 #include <Windows.h>
00007 #else
00008 #include <unistd.h>
00009 #endif
00010 #include "debug.h"
00011 #include <iostream>
00012 #ifdef __APPLE__
00013 #include <execinfo.h>
00014 #endif
00015 #include <stdio.h>  /* defines FILENAME_MAX */
00016 
00017 
00018 #include "glm/glm.hpp"
00019 
00020 using namespace std;
00021 
00022 namespace kick {
00023     namespace {
00024         void print(std::string type, std::string message, std::string func, std::string file, int line, ostream& out = cout) {
00025             out << type << " in "<<file<<":"<< line<<" in "<< func <<"()\n";
00026             out << message << endl;
00027         }
00028     }
00029 
00030     std::function<void (std::string, std::string, std::string, int)> Debug::info = [](std::string message, std::string func, std::string file, int line){
00031         print("info", message, func, file, line);
00032     };
00033     std::function<void (std::string, std::string, std::string, int)> Debug::warning = [](std::string message, std::string func, std::string file, int line){
00034         print("warning", message, func, file, line);
00035     };
00036     std::function<void (std::string, std::string, std::string, int)> Debug::error = [](std::string message, std::string func, std::string file, int line){
00037         print("error", message, func, file, line, cerr);
00038     };
00039 
00040     void Debug::disable() {
00041         function<void (std::string, std::string, std::string, int)> voidFn = [](std::string message, std::string func, std::string file, int line){};
00042         info = voidFn;
00043         warning = voidFn;
00044         error = voidFn;
00045     }
00046 
00047     void Debug::reset() {
00048         info = [](std::string message, std::string func, std::string file, int line){
00049             print("info", message, func, file, line);
00050         };
00051         warning = [](std::string message, std::string func, std::string file, int line){
00052             print("warning", message, func, file, line);
00053         };
00054         error = [](std::string message, std::string func, std::string file, int line){
00055             print("error", message, func, file, line, cerr);
00056         };
00057     }
00058 
00059         string Debug::workingDir(){
00060                 char cCurrentPath[FILENAME_MAX];
00061 
00062 #ifdef _WIN32
00063                 if (!GetCurrentDirectoryA(FILENAME_MAX, cCurrentPath))
00064 #else
00065                 if (!getcwd(cCurrentPath, sizeof(cCurrentPath)))
00066 #endif
00067                 {
00068                         return "Error";
00069                 }
00070 
00071                 return std::string(cCurrentPath);
00072         }
00073 
00074     // based on http://oroboro.com/stack-trace-on-crash/
00075     void Debug::printStacktrace(){
00076 #ifdef __APPLE__
00077         FILE *out = stderr;
00078         unsigned int max_frames = 63;
00079         fprintf(out, "stack trace:\n");
00080 
00081         // storage array for stack trace address data
00082         void* addrlist[max_frames+1];
00083 
00084         // retrieve current stack addresses
00085         glm::u32 addrlen = backtrace( addrlist, sizeof( addrlist ) / sizeof( void* ));
00086 
00087         if ( addrlen == 0 )
00088         {
00089             fprintf( out, "  \n" );
00090             return;
00091         }
00092 
00093         // create readable strings to each frame.
00094         char** symbollist = backtrace_symbols( addrlist, addrlen );
00095 
00096         // print the stack trace.
00097         for ( glm::u32 i = 4; i < addrlen; i++ )
00098             fprintf( out, "%s\n", symbollist[i]);
00099 
00100         free(symbollist);
00101 
00102 #endif
00103     }
00104 
00105     void Debug::drawLine(glm::vec3 from, glm::vec3 to, float seconds, glm::vec4 color, Camera* camera) {
00106         cout << "Implement draw line"<<endl; // todo
00107     }
00108 
00109     void Debug::drawSphere(glm::vec3 center, float size, float seconds, glm::vec4 color, Camera* camera) {
00110         cout << "Implement draw sphere"<<endl; // todo
00111     }
00112 
00113     void Debug::drawBox(glm::vec3 center, glm::vec3 size, float seconds, glm::vec4 color, Camera* camera) {
00114         cout << "Implement draw box"<<endl; // todo
00115     }
00116 }
 All Classes Functions Variables