Skip to content

Commit aa13202

Browse files
Nikita Nazarovnikita-nazarov
authored andcommitted
Add ability for logger to change output streams
1 parent 560f48b commit aa13202

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

src/agent.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ static jboolean setAllocationSamplingMode(jvmtiEventMode mode) {
5656
}
5757

5858
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
59-
handleOptions(options);
59+
logger::handleOptions(options);
6060

61-
debug("on agent load");
61+
logger::debug("on agent load");
6262
jvmtiEnv *jvmti = nullptr;
6363
jint result = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_0);
6464
if (result != JNI_OK || jvmti == nullptr) {
6565
std::cerr << "ERROR: Unable to access JVMTI!" << std::endl;
6666
return result;
6767
}
6868

69-
debug("set capabilities");
69+
logger::debug("set capabilities");
7070
jvmtiCapabilities capabilities;
7171
setRequiredCapabilities(jvmti, capabilities);
7272
jvmtiError error = jvmti->AddCapabilities(&capabilities);
@@ -83,15 +83,15 @@ JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved)
8383
}
8484
}
8585

86-
debug("set callbacks");
86+
logger::debug("set callbacks");
8787
jvmtiEventCallbacks callbacks;
8888
std::memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
8989
callbacks.SampledObjectAlloc = SampledObjectAlloc;
9090
jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));
9191

9292
gdata = new GlobalAgentData();
9393
gdata->jvmti = jvmti;
94-
debug("initializing done");
94+
logger::debug("initializing done");
9595
return JNI_OK;
9696
}
9797

src/log.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <string>
44
#include <iostream>
5+
#include <fstream>
56
#include "log.h"
67

78
namespace logger {
@@ -16,34 +17,45 @@ namespace logger {
1617

1718
LogLevel LEVEL = ERROR;
1819
std::chrono::steady_clock::time_point timePoint;
20+
std::ostream *out = &std::cout;
21+
22+
void open(const char *fileName) {
23+
out = new std::ofstream(fileName);
24+
}
25+
26+
void close() {
27+
if (out != &std::cout) {
28+
delete out;
29+
}
30+
}
1931

2032
void fatal(const char *msg) {
2133
if (LEVEL >= FATAL) {
22-
std::cerr << "MEMORY_AGENT::FATAL " << msg << std::endl;
34+
*out << "MEMORY_AGENT::FATAL " << msg << std::endl;
2335
}
2436
}
2537

2638
void error(const char *msg) {
2739
if (LEVEL >= ERROR) {
28-
std::cerr << "MEMORY_AGENT::ERROR " << msg << std::endl;
40+
*out << "MEMORY_AGENT::ERROR " << msg << std::endl;
2941
}
3042
}
3143

3244
void warn(const char *msg) {
3345
if (LEVEL >= WARN) {
34-
std::cout << "MEMORY_AGENT::WARN " << msg << std::endl;
46+
*out << "MEMORY_AGENT::WARN " << msg << std::endl;
3547
}
3648
}
3749

3850
void info(const char *msg) {
3951
if (LEVEL >= INFO) {
40-
std::cout << "MEMORY_AGENT::INFO " << msg << std::endl;
52+
*out << "MEMORY_AGENT::INFO " << msg << std::endl;
4153
}
4254
}
4355

4456
void debug(const char *msg) {
4557
if (LEVEL >= DEBUG) {
46-
std::cout << "MEMORY_AGENT::DEBUG " << msg << std::endl;
58+
*out << "MEMORY_AGENT::DEBUG " << msg << std::endl;
4759
}
4860
}
4961

@@ -56,7 +68,7 @@ namespace logger {
5668
long long passedTime = std::__1::chrono::duration_cast<std::chrono::milliseconds>(
5769
std::chrono::steady_clock::now() - timePoint
5870
).count();
59-
std::cout << "MEMORY_AGENT::TIME_MS" << passedTime << std::endl;
71+
*out << "MEMORY_AGENT::TIME_MS " << passedTime << std::endl;
6072
}
6173
}
6274

src/log.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ namespace logger {
1919
void logPassedTime();
2020

2121
void handleOptions(const char *options);
22+
23+
void open(const char *fileName);
24+
25+
void close();
2226
}
2327

2428
#endif //MEMORY_AGENT_LOG_H

0 commit comments

Comments
 (0)