home *** CD-ROM | disk | FTP | other *** search
- /***
- *
- * DbgPrintF.c - Formatted printing to the Transcript application.
- *
- * Copyright © 1996 Christopher E. Hyde. All rights reserved.
- *
- * Version: 1.0b1
- *
- ***/
-
- #include <PPCToolbox.h>
- #include <Processes.h>
- #include <AppleEvents.h>
- #include <AERegistry.h>
-
- #include <stdio.h>
- #include <stdarg.h>
-
- #include "DbgPrintF.h"
-
-
- #define FailOSErr(s) if ((s) != noErr) return
-
-
- static void FindAProcess (OSType typeToFind, OSType creatorToFind,
- ProcessSerialNumberPtr processSN, ProcessInfoRecPtr infoRecToFill);
- static void InitDebugPrint (void);
-
-
- // This runs through the process list looking for the indicated application
- static void
- FindAProcess (OSType typeToFind, OSType creatorToFind,
- ProcessSerialNumberPtr processSN, ProcessInfoRecPtr infoRecToFill)
- {
- processSN->lowLongOfPSN = kNoProcess;
- processSN->highLongOfPSN = kNoProcess;
-
- do {
- FailOSErr(GetNextProcess(processSN));
- FailOSErr(GetProcessInformation(processSN, infoRecToFill));
- } while (infoRecToFill->processSignature != creatorToFind ||
- infoRecToFill->processType != typeToFind);
- }
-
-
- static AppleEvent pDbgEvent = { typeNull, nil };
-
- enum {
- // kAEMiscStandards = 'misc',
- kAEEcho = 'Echo',
- kTranscriptType = 'APPL',
- kTranscriptCreator = 'Trns'
- };
-
-
- static void
- InitDebugPrint (void)
- {
- Str31 processName;
- FSSpec procSpec;
- ProcessInfoRec infoRec;
- ProcessSerialNumber process;
- AEDesc targetDesc = { typeNull, nil };
-
- infoRec.processInfoLength = sizeof(ProcessInfoRec);
- infoRec.processName = processName;
- infoRec.processAppSpec = &procSpec;
-
- // Find the 'Transcript' application on the machine we wish to communicate with
- FindAProcess(kTranscriptType, kTranscriptCreator, &process, &infoRec);
-
- FailOSErr(AECreateDesc(typeProcessSerialNumber, &process, sizeof(process), &targetDesc));
-
- // Create the EchoEvent
- FailOSErr(AECreateAppleEvent(kAEMiscStandards, kAEEcho,
- &targetDesc, kAutoGenerateReturnID,
- kAnyTransactionID, &pDbgEvent));
-
- AEDisposeDesc(&targetDesc);
- }
-
-
- void
- DbgPrintF (const char* format, ...)
- {
- static Ptr pMsgBuf = nil;
- va_list ap;
- Size msgLength;
- AppleEvent aeReply;
-
- if (pMsgBuf == nil) {
- InitDebugPrint();
- pMsgBuf = NewPtr(4096);
- if (pMsgBuf == nil)
- return;
- }
-
- va_start(ap, format);
- msgLength = vsprintf(pMsgBuf, format, ap);
- va_end(ap)
-
- FailOSErr(AEPutParamPtr(&pDbgEvent, keyDirectObject, typeChar, pMsgBuf, msgLength));
-
- FailOSErr(AESend(&pDbgEvent, &aeReply, kAENoReply + kAENeverInteract + kAECanSwitchLayer,
- kAENormalPriority, kAEDefaultTimeout, nil, nil));
- FailOSErr(AEDeleteParam(&pDbgEvent, keyDirectObject));
- }
-
-