Ignore:
Timestamp:
Feb 12, 2007, 9:35:45 PM (19 years ago)
Author:
RBRi
Message:

improved debug switch handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/NewView/DebugUnit.pas

    r70 r76  
    22
    33// NewView - a new OS/2 Help Viewer
    4 // Copyright 2006 Ronald Brill (rbri at rbri dot de)
     4// Copyright 2006/2007 Ronald Brill (rbri at rbri dot de)
    55// This software is released under the GNU Public License - see readme.txt
    66
     
    88
    99Interface
    10 
    11 {$define DEBUG}
    1210
    1311
     
    1816  SysUtils;
    1917
    20 {$ifdef DEBUG}
    21 imports
    22   Function PmPrintfString(aString:PChar):BYTE; APIENTRY; 'PMPRINTF' NAME 'PmPrintfString';
    23 end;
    24 {$endif}
    2518
    2619  // -- Logging --
     
    5447  // Procedure PrfTraceEvent(const anEventDescription: String);
    5548
    56 const
    57   activeLogAspects : LogAspects = [
    58 //                                        LogStartup,
    59 //                                        LogShutdown,
    60 //                                        LogSettings,
    61 //                                        LogParse,
    62 //                                        LogDisplay,
    63 //                                        LogSearch,
    64                                           LogNHM,
    65 //                                        LogViewStub,
    66 //                                        LogObjConstDest,
    67                                           LogDebug
    68                                   ];
     49  Procedure SetLogAspects(const aCommaSeparatedListOfAspectNames : String);
     50
    6951
    7052var
    7153  startTime : ULONG;
    7254  lastTime : ULONG;
     55  PMPrintfModuleHandle : HMODULE;
     56  PMPrintfString : Function(aString : PChar) : ULONG; APIENTRY;
     57  activeLogAspects : LogAspects;
     58
     59
    7360
    7461Implementation
     62uses
     63  BseDos,
     64  StringUtilsUnit;
     65
     66  FUNCTION LoadPMPrinfFLib : integer;
     67  Var
     68    tmpDllName : CString;
     69    tmpErrorInfo : CString;
     70    tmpProcedureName : CSTRING;
     71    tmpProcedureAddress : POINTER;
     72    tmpRC : APIRET;
     73
     74  begin
     75    PMPrintfString := nil;
     76
     77    tmpDllName:='PMPRINTF';
     78    tmpRC := DosLoadModule(tmpErrorInfo, 255, tmpDllName, PMPrintfModuleHandle);
     79    if tmpRC <> 0 then
     80    begin
     81        PMPrintfModuleHandle := 0;
     82        result := 0;
     83        exit;
     84    end;
     85
     86    tmpProcedureName := 'PmPrintfString';
     87    tmpRC := DosQueryProcAddr(PMPrintfModuleHandle, 0, tmpProcedureName, tmpProcedureAddress);
     88    if tmpRC <> 0 then
     89    begin
     90      DosFreeModule(PMPrintfModuleHandle);
     91      PMPrintfModuleHandle := 0;
     92      result := 0;
     93      exit;
     94    end;
     95
     96    PMPrintfString := tmpProcedureAddress;
     97    result := 0;
     98  end;
     99
     100
     101  Procedure PMPrintf(const aString : String);
     102  Var
     103    tmpPCharMessage : PChar;
     104  Begin
     105    if (0 <> PMPrintfModuleHandle) then
     106    begin
     107      tmpPCharMessage := StrAlloc(length(aString) + 1);
     108      StrPCopy(tmpPCharMessage, aString);
     109
     110      PmPrintfString(tmpPCharMessage);
     111
     112      StrDispose(tmpPCharMessage);
     113    end;
     114  end;
     115
    75116
    76117  Function GetAspectPrefix(const aLogAspect: LogAspect): String;
     
    92133
    93134
     135  Procedure SetLogAspects(const aCommaSeparatedListOfAspectNames : String);
     136  Var
     137    tmpAspects : TStringList;
     138    i : Integer;
     139  Begin
     140    tmpAspects := TStringList.Create;
     141    StrExtractStrings(tmpAspects, aCommaSeparatedListOfAspectNames, [','], #0);
     142
     143    for i:=0 to tmpAspects.count-1 do
     144    begin
     145      if tmpAspects[i] = 'LogStartup'      then activeLogAspects := activeLogAspects + [ LogStartup ];
     146      if tmpAspects[i] = 'LogShutdown'     then activeLogAspects := activeLogAspects + [ LogShutdown ];
     147      if tmpAspects[i] = 'LogSettings'     then activeLogAspects := activeLogAspects + [ LogSettings ];
     148      if tmpAspects[i] = 'LogParse'        then activeLogAspects := activeLogAspects + [ LogParse ];
     149      if tmpAspects[i] = 'LogDisplay'      then activeLogAspects := activeLogAspects + [ LogDisplay ];
     150      if tmpAspects[i] = 'LogSearch'       then activeLogAspects := activeLogAspects + [ LogSearch ];
     151      if tmpAspects[i] = 'LogNHM'          then activeLogAspects := activeLogAspects + [ LogNHM ];
     152      if tmpAspects[i] = 'LogViewStub'     then activeLogAspects := activeLogAspects + [ LogViewStub ];
     153      if tmpAspects[i] = 'LogObjConstDest' then activeLogAspects := activeLogAspects + [ LogObjConstDest ];
     154      if tmpAspects[i] = 'LogDebug'        then activeLogAspects := activeLogAspects + [ LogDebug ];
     155    end;
     156
     157    tmpAspects.Destroy;
     158  End;
     159
     160
    94161  Procedure LogEvent(const aLogAspect: LogAspect; const anEventDescription: String);
    95 {$ifdef DEBUG}
    96162  Var
    97163    tmpMessage : String;
    98     tmpPCharMessage : PChar;
    99 {$endif}
    100   Begin
    101 {$ifdef DEBUG}
     164  Begin
    102165    if (aLogAspect IN activeLogAspects) then
    103166    begin
    104167      tmpMessage := 'Log[' + GetAspectPrefix(aLogAspect) + ']  ' + anEventDescription;
    105 
    106       tmpPCharMessage := StrAlloc(length(tmpMessage) + 1);
    107       StrPCopy(tmpPCharMessage, tmpMessage);
    108 
    109       PmPrintfString(tmpPCharMessage);
    110       StrDispose(tmpPCharMessage);
    111     end;
    112 {$endif}
    113   end;
    114 
    115 
     168      PmPrintf(tmpMessage);
     169    end;
     170  end;
    116171
    117172
     
    124179  Procedure PrfStartTimer;
    125180  Begin
    126 {$ifdef DEBUG}
    127181    startTime := GetSystemMSCount;
    128182    lastTime := startTime;
    129 {$endif}
    130   End;
     183  End;
     184
    131185
    132186  Procedure PrfStopTimer;
     
    136190
    137191  Procedure PrfTraceEvent(const anEventDescription: String);
    138 {$ifdef DEBUG}
    139192  Var
    140193    tmpTime : ULONG;
    141194    tmpMessage : String;
    142     tmpPCharMessage : PChar;
    143 {$endif}
    144   Begin
    145 {$ifdef DEBUG}
     195  Begin
    146196    tmpTime := GetSystemMSCount;
    147197    tmpMessage := 'Prf: ' + IntToStr(tmpTime - lastTime) + 'ms ' + anEventDescription + IntToStr(tmpTime - startTime) + 'ms since start';
    148198
    149     tmpPCharMessage := StrAlloc(length(tmpMessage) + 1);
    150     StrPCopy(tmpPCharMessage, tmpMessage);
    151 
    152     PmPrintfString(tmpPCharMessage);
    153     StrDispose(tmpPCharMessage);
     199    PMPrintf(tmpMessage);
    154200
    155201    lastTime := GetSystemMSCount;
    156 {$endif}
    157   end;
     202  end;
     203
     204
     205  Initialization
     206    LoadPMPrinfFLib;
    158207END.
Note: See TracChangeset for help on using the changeset viewer.