Chinaunix首页 | 论坛 | 博客
  • 博客访问: 210477
  • 博文数量: 145
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 1720
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-14 18:42
文章分类

全部博文(145)

文章存档

2011年(1)

2009年(144)

我的朋友

分类: LINUX

2009-07-22 20:09:25

by tangke <>   2009-06-02

1.版本信息

一般我们都会写程序的Changelog,但是对于一个软件的使用,我觉得如果作者把Changelog信息写在程序里面的话,那么对于使用者来说是一个很大的方便,因为普通的使用者并不像开发者一样要去查source code的Changelog.
对应于Qt编程,主要分为几个方面的工作:
1.在资源文件qrc文件加入Changelog文件的支持。
2.对命令行参数进行处理,并且使用qInfo来遍历Changlog文件并且打印出所有的Changlog信息。
3.可以将这部分功能做成一个模块so包,供其他Qt程序进行调用。
4.在以后的基础上面可以做成一个通用包,而不是针对Qt程序。

2.打印信息

我们有时候需要将打印信息输出到一个文件里面,一般通过-o /tmp/XXX.log来指定。在qt里面我们也可以这样做。

localSystem::initialize();
void initialize( void )
{
   localSystem::initialize( NULL, "italc_master.log" );
#ifdef BUILD_WIN32
   if( QDir( "C:\\WINDOWS" ).exists() == FALSE )
   { 
       QDir( "C:\\" ).mkdir( "WINDOWS" );
   } 
#endif
}
void initialize( p_pressKey _pk, const QString & _log_file )
{
   __pressKey = _pk;
   __log_file = _log_file;
   lzo_init();
   QCoreApplication::setOrganizationName( "iTALC Solutions" );
   QCoreApplication::setOrganizationDomain( "italcsolutions.org" );
   QCoreApplication::setApplicationName( "iTALC" );
   QSettings settings( QSettings::SystemScope, "iTALC Solutions", "iTALC" );
 
   if( settings.contains( "settings/LogLevel" ) )
   {
       logLevel = settings.value( "settings/LogLevel" ).toInt();
   }
   qInstallMsgHandler( msgHandler );
   initResources();
}
void msgHandler( QtMsgType _type, const char * _msg )
{
   if( localSystem::logLevel == 0 )
   {
       return ;
   }
#ifdef BUILD_WIN32
   if( QString( _msg ).contains( "timers cannot be stopped",
                           Qt::CaseInsensitive ) )
   {
       exit( 0 );
   }
#endif
   if( __debug_out == NULL )
   {
       QString tmp_path = QDir::rootPath() +
#ifdef BUILD_WIN32
                       "temp"
#else
                       "tmp"
#endif
               ;
       foreach( const QString s, QProcess::systemEnvironment() )
       {
           if( s.toLower().left( 5 ) == "temp=" )
           {
               tmp_path = s.toLower().mid( 5 );
               break;
           }
           else if( s.toLower().left( 4 ) == "tmp=" )
           {
               tmp_path = s.toLower().mid( 4 );
               break;
           }
       }
      if( !QDir( tmp_path ).exists() )
       {
           if( QDir( QDir::rootPath() ).mkdir( tmp_path ) )
           {
               QFile::setPermissions( tmp_path,
                       QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
                       QFile::ReadUser | QFile::WriteUser | QFile::ExeUser |
                       QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
                       QFile::ReadOther | QFile::WriteOther | QFile::ExeOther );
           }
       }
       const QString log_path = tmp_path + QDir::separator();
       __debug_out = new QFile( log_path + __log_file );
       __debug_out->open( QFile::WriteOnly | QFile::Append |
                           QFile::Unbuffered );
   }
   QString out;
   switch( _type )
   {
       case QtDebugMsg:
           if( localSystem::logLevel > 8)
           {
               out = QDateTime::currentDateTime().toString() + QString( ": [debug] %1" ).arg( _msg ) + "\n";
           }
           break;
       case QtWarningMsg:
           if( localSystem::logLevel > 5 )
           {
               out = QDateTime::currentDateTime().toString() + QString( ": [warning] %1" ).arg( _msg ) + "\n";
           }
          break;
       case QtCriticalMsg:
           if( localSystem::logLevel > 3 )
           {
               out = QDateTime::currentDateTime().toString() + QString( ": [critical] %1" ).arg( _msg ) + "\n";
           }
           break;
       case QtFatalMsg:
           if( localSystem::logLevel > 1 )
           {
               out = QDateTime::currentDateTime().toString() + QString( ": [fatal] %1" ).arg( _msg ) + "\n";
           }
       default:
           out = QDateTime::currentDateTime().toString() + QString( ": [unknown] %1" ).arg( _msg ) + "\n";
           break;
   }
   if( out.trimmed().size() )
   {
       out = properLineEnding( out );
       __debug_out->write( out.toUtf8() );
       printf( "%s", out.toUtf8().constData() );
   }
}
阅读(565) | 评论(0) | 转发(0) |
0

上一篇:Qt 窗口几何结构

下一篇:android 网站[1]

给主人留下些什么吧!~~