Clapkit
Loading...
Searching...
No Matches
ckUtils.h
Go to the documentation of this file.
1/*
2 *
3 * Clapkit
4 * ----------------------------------------------------------------------
5 * A wrapper for creating a 'generalized' app for Classic MacOS
6 * that (hopefully) can be ported easily to other platforms.
7 *
8 * CKUtils
9 * ----------------------------------------------------------------------
10 * Utilies for commonly done actions.
11 *
12 */
13
14#pragma once
15
16#include "ckMemory.h"
17#include "dlmalloc.h"
18#include <cstddef>
19#include <cstdlib>
20#include <cstring>
21#include <stdarg.h>
22#include <stdio.h>
23
24#ifdef kCKAPPDEBUG
25#define __FILENAME__ __FILE__
26#else
27#define __FILENAME__ ""
28#endif
29
34#define CKMSToTicks(milliseconds) ((int)((milliseconds) / 16.66))
35
36#ifdef kCKAPPDEBUG
37
45#define CKLog(s, ...) __CKDebugLog(0, s __VA_OPT__(, ) __VA_ARGS__)
46#define CKDebugLog(l, s, ...) __CKDebugLog(l, s __VA_OPT__(, ) __VA_ARGS__)
47#else
48
53#define CKLog(s, ...) \
54 do { \
55 } while (0)
56#define CKDebugLog(l, s, ...) \
57 do { \
58 } while (0)
59#endif
60
61unsigned char* __CKC2P(const char* src, const char* func, int line, const char* file);
62char* __CKP2C(const unsigned char* src, const char* func, int line, const char* file);
63
64#ifdef kCKAPPDEBUG
70#define CKC2P(s) __CKC2P(s, __func__, __LINE__, __FILENAME__)
71
77#define CKP2C(s) __CKP2C(s, __func__, __LINE__, __FILENAME__)
78#else
83#define CKC2P(s) __CKC2P(s, "", 0, "")
84
90#define CKP2C(s) __CKP2C(s, "", 0, "")
91#endif
92
93void __CKDebugLog(int level, const char* s, ...);
94void CKConsolePrint(const char* toPrint);
95
99#ifdef kCKAPPDEBUG
100#include <vector>
102 public:
103 char* name;
105 int calls;
106};
107extern std::vector<CKProfilerData*> _profilerData;
108extern bool _profilerDataInit;
110 public:
111 CKScopeProfiler(const char* fName) {
112 this->name = (char*)dlmalloc(strlen(fName) + 1);
113 strcpy(this->name, fName);
114 // CKLog("PROFILE: %s started.", this->name);
115 this->start = TickCount();
116 }
118 long time = (TickCount() - this->start);
119 // CKLog("PROFILE: %s ended. Took %lu ticks.", this->name, time);
120 bool found = false;
121 if (!_profilerDataInit) {
122 _profilerData = std::vector<CKProfilerData*>();
123 _profilerDataInit = true;
124 }
125 for (auto& d : _profilerData) {
126 if (strcmp(d->name, this->name) == 0) {
127 d->totalTime += time;
128 d->calls++;
129 found = true;
130 }
131 }
132 if (!found) {
133 CKProfilerData* nd = new CKProfilerData();
134 nd->name = (char*)dlmalloc(strlen(this->name) + 1);
135 strcpy(nd->name, this->name);
136 nd->totalTime = time;
137 nd->calls = 1;
138 _profilerData.push_back(nd);
139 }
140 dlfree(this->name);
141 }
142
143 private:
144 char* name;
145 long start;
146};
147void CKPrintProfileData();
149void __CKWriteToExitFile(const char* s, ...);
150#define CKPROFILE CKScopeProfiler __p(__PRETTY_FUNCTION__);
151#else
152#define CKPROFILE
153#endif
154
156UInt32 CKMillis();
void __CKWriteToExitFile(const char *s,...)
Definition ckUtils.cpp:164
std::vector< CKProfilerData * > _profilerData
Definition ckUtils.cpp:20
void __CKDebugLog(int level, const char *s,...)
Definition ckUtils.cpp:77
void CKPrintExitDebugData()
Definition ckUtils.cpp:198
bool _profilerDataInit
Definition ckUtils.cpp:21
void CKConsolePrint(const char *toPrint)
Definition ckUtils.cpp:143
char * __CKP2C(const unsigned char *src, const char *func, int line, const char *file)
Definition ckUtils.cpp:54
unsigned char * __CKC2P(const char *src, const char *func, int line, const char *file)
Definition ckUtils.cpp:28
void CKPrintProfileData()
Definition ckUtils.cpp:229
Definition ckUtils.h:101
long totalTime
Definition ckUtils.h:104
int calls
Definition ckUtils.h:105
char * name
Definition ckUtils.h:103
Definition ckUtils.h:109
~CKScopeProfiler()
Definition ckUtils.h:117
CKScopeProfiler(const char *fName)
Definition ckUtils.h:111
bool CKHasAppearanceManager()
Checks if Appearance Manager is present (8.x+)
Definition ckUtils.cpp:257
UInt32 CKMillis()
Return the number of milliseconds since computer booted up.
Definition ckUtils.cpp:268