home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / am4pmsrc.zip / pmlog.c < prev    next >
C/C++ Source or Header  |  1994-02-27  |  6KB  |  341 lines

  1. // Thomas Answering machine for PM
  2.  
  3. // File:          PMLOG.h
  4. // Description:   Routines for debug printing
  5.  
  6. // History
  7. // 930213 TO      Now it exists...
  8.  
  9.  
  10. #define INCL_WIN
  11. #define INCL_BASE
  12. #include <os2.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <time.h>
  18.  
  19. #include "am4pm.h"
  20.  
  21. static HQUEUE hDebQueue, hRxSIOQueue;
  22. static PID pidDeb, pidRxSIO;
  23. static BOOL fRxSIO = FALSE;
  24.  
  25. void InitDebug(void)
  26. {
  27.    USHORT res;
  28.          
  29.    res=DosOpenQueue(&pidDeb, &hDebQueue, "\\queues\\vfedeb");
  30.    fDebug = res==0;
  31. }
  32.  
  33.  
  34. void dprintf
  35. (
  36.    PCHAR szStr,
  37.    ...
  38. )
  39. {
  40.    va_list arg_marker;
  41.    PVOID pSendBuff;
  42.    USHORT res;
  43.  
  44.    if (!fDebug)
  45.       return;
  46.  
  47.    res=DosAllocSharedMem(&pSendBuff, NULL, 200, PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_GIVEABLE);
  48.    if (res)
  49.    {
  50.       fDebug=FALSE;
  51.       return;
  52.    }
  53.  
  54.    va_start(arg_marker, szStr);
  55.  
  56.    vsprintf(pSendBuff, szStr, arg_marker);
  57.  
  58.    va_end(arg_marker);
  59.  
  60.    res=DosGiveSharedMem(pSendBuff, pidDeb, PAG_READ | PAG_WRITE);
  61.    if (res)
  62.    {
  63.       fDebug=FALSE;
  64.       return;
  65.    }
  66.  
  67.    res=DosFreeMem(pSendBuff);
  68.    if (res)
  69.    {
  70.       fDebug=FALSE;
  71.       return;
  72.    }
  73.  
  74.    res=DosWriteQueue(hDebQueue, 0, 200, pSendBuff, 0);
  75.    if (res)
  76.    {
  77.       fDebug=FALSE;
  78.       return;
  79.    }
  80.  
  81. }
  82.  
  83.  
  84. USHORT ShowMessage
  85. (
  86.    USHORT usMsgNr,
  87.    HWND hwnd,
  88.    USHORT flStyle,
  89.    USHORT idHelp,
  90.    ...
  91. )
  92. {
  93.    va_list arg_marker;
  94.    ULONG ulLen;
  95.    CHAR szStr[200], szStr2[200];
  96.  
  97.    if (idHelp==0)
  98.       idHelp=usMsgNr + 10000;
  99.  
  100.    va_start(arg_marker, idHelp);
  101.  
  102.    DosGetMessage(NULL, 0, szStr, 200, usMsgNr, szMsgFile, &ulLen);
  103.    if (szStr[ulLen-1]=='\n')
  104.       szStr[ulLen-2]='\0';
  105.    else
  106.       szStr[ulLen]='\0';
  107.  
  108.    vsprintf(szStr2, szStr, arg_marker);
  109.  
  110.    va_end(arg_marker);
  111.  
  112.    return WinMessageBox(HWND_DESKTOP, hwnd, szStr2, szAppName, idHelp, flStyle | MB_MOVEABLE);
  113. }
  114.  
  115.  
  116. static void PrintString
  117. (
  118.    PCHAR szStr,
  119.    ULONG ulLen
  120. )
  121. {
  122.    HFILE hFile;
  123.    USHORT res;
  124.    ULONG ulPos, ulAction;
  125.    static CHAR szFileError[]="Error opening logfile!!\r\n";
  126.  
  127.    DosPutMessage(2, ulLen, szStr);
  128.    res=DosOpen("am4pm.log", &hFile, &ulAction, 0l, FILE_NORMAL, FILE_OPEN | FILE_CREATE, OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE, 0l);
  129.    if (res)
  130.    {
  131.       DosPutMessage(2, sizeof szFileError, szFileError);
  132.    }
  133.    else
  134.    {
  135.       DosChgFilePtr(hFile, 0l, FILE_END, &ulPos);
  136.       DosPutMessage(hFile, ulLen, szStr);
  137.       DosClose(hFile);
  138.    }
  139. }
  140.  
  141.  
  142. static void PrintMessage
  143. (
  144.    USHORT usMsgNr,
  145.    PCHAR szMsgFileName,
  146.    va_list arg_marker
  147. )
  148. {
  149.    PCHAR ppBuff[10];
  150.    USHORT usNr=0;
  151.    CHAR szStr[200];
  152.    ULONG ulLen;
  153.  
  154.    while (ppBuff[usNr]=va_arg(arg_marker, PCHAR))
  155.       usNr++;
  156.  
  157.    DosGetMessage(ppBuff, usNr, szStr, 200, usMsgNr, szMsgFileName, &ulLen);
  158.    PrintString(szStr, ulLen);
  159.    if (szStr[ulLen-1]=='\n')
  160.       szStr[ulLen-2]='\0';
  161.    else
  162.       szStr[ulLen]='\0';
  163. // WinPostMsg(hwndClient, WM_NEWMSG, MPFROMP(strdup(szStr)), 0l);
  164. }
  165.  
  166.  
  167. static void PrintNumMessage
  168. (
  169.    USHORT usMsgNr,
  170.    PCHAR szMsgFileName,
  171.    USHORT usNum,
  172.    va_list arg_marker
  173. )
  174. {
  175.    PCHAR ppBuff[10];
  176.    USHORT usNr=1;
  177.    CHAR szStr[200], szNum[10];
  178.    ULONG ulLen;
  179.  
  180.    ppBuff[0]=_itoa(usNum, szNum, 10);
  181.    while (ppBuff[usNr]=va_arg(arg_marker, PCHAR))
  182.       usNr++;
  183.  
  184.    DosGetMessage(ppBuff, usNr, szStr, 200, usMsgNr, szMsgFileName, &ulLen);
  185.    PrintString(szStr, ulLen);
  186.    if (szStr[ulLen-1]=='\n')
  187.       szStr[ulLen-2]='\0';
  188.    else
  189.       szStr[ulLen]='\0';
  190. // WinPostMsg(hwndClient, WM_NEWMSG, MPFROMP(strdup(szStr)), 0l);
  191. }
  192.  
  193.  
  194.  
  195. static void LogPrintTime
  196. (
  197.    void
  198. )
  199. {
  200.    long ltime;
  201.    CHAR szMsg[200], * szTime;
  202.  
  203.    strcpy(szMsg, "\r\n** ");
  204.    time(<ime);
  205.    szTime=ctime(<ime);
  206.    strcat(szMsg, szTime);
  207.    szMsg[strlen(szMsg)-1]='\0';
  208.    strcat(szMsg, " **\r\n");
  209.    PrintString(szMsg, strlen(szMsg));
  210. // WinPostMsg(hwndClient, WM_NEWMSG, MPFROMP(strdup("")), 0l);
  211.    strcpy(szMsg, szTime);
  212.    szMsg[strlen(szMsg)-1]='\0';
  213. // WinPostMsg(hwndClient, WM_NEWMSG, MPFROMP(strdup(szMsg)), 0l);
  214. }
  215.  
  216.  
  217. void LogMessage
  218. (
  219.    USHORT usMsgNr,
  220.    ...
  221. )
  222. {
  223.    va_list arg_marker;
  224.  
  225.    va_start(arg_marker, usMsgNr);
  226.  
  227.    LogPrintTime();
  228.    PrintMessage(usMsgNr, szMsgFile, arg_marker);
  229. }
  230.  
  231. #if 0
  232. Not used for time being
  233. static void LogPrintLine
  234. (
  235.    USHORT usMsgNr,
  236.    ...
  237. )
  238. {
  239.    va_list arg_marker;
  240.  
  241.    va_start(arg_marker, usMsgNr);
  242.  
  243.    PrintMessage(usMsgNr, szMsgFile, arg_marker);
  244. }
  245. #endif
  246.  
  247.  
  248. void LogNumMessage
  249. (
  250.    USHORT usMsgNr,
  251.    USHORT usNum,
  252.    ...
  253. )
  254. {
  255.    va_list arg_marker;
  256.  
  257.    va_start(arg_marker, usNum);
  258.  
  259.    LogPrintTime();
  260.    PrintNumMessage(usMsgNr, szMsgFile, usNum, arg_marker);
  261. }
  262.  
  263.  
  264. void LogDosMessage
  265. (
  266.    USHORT usDosErr,
  267.    USHORT usMsgNr,
  268.    ...
  269. )
  270. {
  271.    va_list arg_marker;
  272.  
  273.    va_start(arg_marker, usMsgNr);
  274.  
  275.    LogPrintTime();
  276.    PrintMessage(usMsgNr, szMsgFile, arg_marker);
  277.    PrintMessage(0, szMsgFile, arg_marker);
  278.    PrintMessage(usDosErr, "oso001.msg", arg_marker);
  279. }
  280.  
  281.  
  282.  
  283. static void InitRxSIO(void)
  284. {
  285.    USHORT res;
  286.    
  287.    res=DosOpenQueue(&pidRxSIO, &hRxSIOQueue, "\\queues\\rxsio");
  288.    fRxSIO = res==0;
  289. }
  290.  
  291.  
  292. void PrintRxSIO
  293. (
  294.    PCHAR szStr
  295. )
  296. {
  297.    PVOID pSendBuff;
  298.    USHORT res, usLen;
  299.    static CHAR szCR[] = "\n";
  300.  
  301.    if (!fRxSIO)
  302.       InitRxSIO();
  303.  
  304.    if (!fRxSIO)
  305.       return;
  306.  
  307.    usLen = strlen(szStr);
  308.  
  309.    res=DosAllocSharedMem(&pSendBuff, NULL, usLen+3, PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_GIVEABLE);
  310.    if (res)
  311.    {
  312.       fRxSIO=FALSE;
  313.       return;
  314.    }
  315.  
  316.    memcpy(pSendBuff, szStr, usLen);
  317.    memcpy(((PCHAR)pSendBuff)+usLen, szCR, sizeof szCR);
  318.  
  319.    res=DosGiveSharedMem(pSendBuff, pidRxSIO, PAG_READ | PAG_WRITE);
  320.    if (res)
  321.    {
  322.       fRxSIO=FALSE;
  323.       return;
  324.    }
  325.  
  326.    res=DosFreeMem(pSendBuff);
  327.    if (res)
  328.    {
  329.       fRxSIO=FALSE;
  330.       return;
  331.    }
  332.  
  333.    res=DosWriteQueue(hRxSIOQueue, 0, usLen+3, pSendBuff, 0);
  334.    if (res)
  335.    {
  336.       fRxSIO=FALSE;
  337.       return;
  338.    }
  339.  
  340. }
  341.