defuze.me  Client
exception.cpp
00001 /**************************************************************************
00002 ** defuze.me Epitech Innovative Project
00003 **
00004 ** Copyright 2010
00005 **   Athena Calmettes - Jocelyn De La Rosa - Francois Gaillard
00006 **   Adrien Jarthon - Alexandre Moore - Luc Peres - Arnaud Sellier
00007 **
00008 ** All rights reserved.
00009 **************************************************************************/
00010 
00011 #include "logger.hpp"
00012 #include "exception.hpp"
00013 #include <QApplication>
00014 #include <QVariant>
00015 #include <QStringList>
00016 #include <QTime>
00017 
00018 Exception::Exception()
00019 {
00020 }
00021 
00022 Exception::Exception(unsigned code, const QString msg, const char *file, const int line)
00023 {
00024     this->_msg = msg;
00025     this->file = file;
00026     this->line = line;
00027     this->userCode = code;
00028     extractCore();
00029     generateCode();
00030     Logger::log("Error at " + QTime::currentTime().toString() + ": " + description());
00031 }
00032 
00033 Exception::~Exception() throw()
00034 {
00035 }
00036 
00037 Exception::Exception(const Exception& src)
00038 {
00039     _msg = src._msg;
00040     file = src.file;
00041     line = src.line;
00042     userCode = src.userCode;
00043     module = src.module;
00044     code = src.code;
00045 }
00046 
00047 void        Exception::raise() const
00048 {
00049     throw *this;
00050 }
00051 
00052 Exception*  Exception::clone() const
00053 {
00054     return new Exception(*this);
00055 }
00056 
00057 void        Exception::extractCore() throw()
00058 {
00059     module = "Base";
00060     QStringList path = file.split("/");
00061     for(int i = 0; i < path.size(); ++i)
00062     {
00063         if (path[i] == "core" && (i+2) < path.size())
00064         {
00065             module = path[i + 1];
00066             module[0] = module[0].toUpper();
00067             module += "Core";
00068         }
00069         if (path[i] == "plugins" && (i+2) < path.size())
00070         {
00071             module = path[i + 1];
00072             module[0] = module[0].toUpper();
00073             module += "Plugin";
00074         }
00075     }
00076 }
00077 
00078 void        Exception::generateCode() throw()
00079 {
00080     code =  (qHash(module) & 0xFF << 16) |
00081             (qHash(file) & 0xFF << 8) |
00082             (userCode & 0xFF);
00083 }
00084 
00085 QString     Exception::hexCode() const throw()
00086 {
00087     return QString("%1").arg(code, 6, 16, QLatin1Char('0')).toUpper();
00088 }
00089 
00090 QString     Exception::description() const throw()
00091 {
00092     return (QString("[%1] %2Exception at %3:%4 -> %5").arg(hexCode(), module, file).arg(line).arg(_msg));
00093 }
00094 
00095 QString     Exception::msg() const throw()
00096 {
00097     return _msg;
00098 }