Class: AerospikeNative::Logger

Inherits:
Object
  • Object
show all
Defined in:
ext/aerospike_native/logger.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject



24
25
26
27
28
29
30
31
# File 'ext/aerospike_native/logger.c', line 24

VALUE logger_initialize(VALUE vSelf)
{
    int default_level = AS_LOG_LEVEL_DEBUG;
    as_log_set_level(default_level);
    as_log_set_callback(aerospike_log_callback);
    rb_iv_set(vSelf, "@level", INT2FIX(default_level));
    return vSelf;
}

Instance Attribute Details

#internalObject

#levelObject (readonly)

Instance Method Details

#debug(vMsg) ⇒ Object



151
152
153
154
# File 'ext/aerospike_native/logger.c', line 151

VALUE logger_debug(VALUE vSelf, VALUE vMsg)
{
    return rb_funcall(vSelf, rb_intern("write"), 2, INT2FIX(AS_LOG_LEVEL_DEBUG), vMsg);
}

#error(vMsg) ⇒ Object



136
137
138
139
# File 'ext/aerospike_native/logger.c', line 136

VALUE logger_error(VALUE vSelf, VALUE vMsg)
{
    return rb_funcall(vSelf, rb_intern("write"), 2, INT2FIX(AS_LOG_LEVEL_ERROR), vMsg);
}

#info(vMsg) ⇒ Object



146
147
148
149
# File 'ext/aerospike_native/logger.c', line 146

VALUE logger_info(VALUE vSelf, VALUE vMsg)
{
    return rb_funcall(vSelf, rb_intern("write"), 2, INT2FIX(AS_LOG_LEVEL_INFO), vMsg);
}

#set_level(vLevel) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'ext/aerospike_native/logger.c', line 33

VALUE logger_set_level(VALUE vSelf, VALUE vLevel)
{
    VALUE vInternalLogger = rb_iv_get(vSelf, "@internal");
    int log_level = AS_LOG_LEVEL_INFO;
    VALUE vError = ID2SYM( rb_intern("error") );
    VALUE vWarn = ID2SYM( rb_intern("warn") );
    VALUE vInfo = ID2SYM( rb_intern("info") );
    VALUE vDebug = ID2SYM( rb_intern("debug") );
    VALUE vTrace = ID2SYM( rb_intern("trace") );

    Check_Type(vLevel, T_SYMBOL);
    if (vLevel == vError) {
        log_level = AS_LOG_LEVEL_ERROR;
    } else if (vLevel == vWarn) {
        log_level = AS_LOG_LEVEL_WARN;
    } else if (vLevel == vInfo) {
        log_level = AS_LOG_LEVEL_INFO;
    } else if (vLevel == vDebug) {
        log_level = AS_LOG_LEVEL_DEBUG;
    } else if (vLevel == vTrace) {
        log_level = AS_LOG_LEVEL_TRACE;
    } else {
        return Qfalse;
    }

    as_log_set_level(log_level);
    rb_iv_set(vSelf, "@level", INT2FIX(log_level));

    if (TYPE(vInternalLogger) != T_NIL) {
        int internal_level = 3 - (log_level == AS_LOG_LEVEL_TRACE ? AS_LOG_LEVEL_DEBUG : log_level);
        rb_iv_set(vInternalLogger, "@level", INT2FIX(internal_level));
    }

    return Qtrue;
}

#trace(vMsg) ⇒ Object



156
157
158
159
# File 'ext/aerospike_native/logger.c', line 156

VALUE logger_trace(VALUE vSelf, VALUE vMsg)
{
    return rb_funcall(vSelf, rb_intern("write"), 2, INT2FIX(AS_LOG_LEVEL_TRACE), vMsg);
}

#warn(vMsg) ⇒ Object



141
142
143
144
# File 'ext/aerospike_native/logger.c', line 141

VALUE logger_warn(VALUE vSelf, VALUE vMsg)
{
    return rb_funcall(vSelf, rb_intern("write"), 2, INT2FIX(AS_LOG_LEVEL_WARN), vMsg);
}

#write(vLevel, vMsg) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/aerospike_native/logger.c', line 69

VALUE logger_write(VALUE vSelf, VALUE vLevel, VALUE vMsg)
{
    int level;
    VALUE vInternalLevel, vInternalLogger;
    ID vMethod;
    Check_Type(vLevel, T_FIXNUM);
    Check_Type(vMsg, T_STRING);

    vInternalLevel = rb_iv_get(vSelf, "@level");
    vInternalLogger = rb_iv_get(vSelf, "@internal");
    Check_Type(vInternalLevel, T_FIXNUM);
    level = FIX2INT(vLevel);

    switch(TYPE(vInternalLogger)) {
    case T_NIL:
        if (level <= FIX2INT(vInternalLevel)) {
            switch(level) {
            case AS_LOG_LEVEL_ERROR:
                vMsg = rb_str_plus(rb_str_new2("ERROR (AEROSPIKE): "), vMsg);
                break;
            case AS_LOG_LEVEL_WARN:
                vMsg = rb_str_plus(rb_str_new2("WARN  (AEROSPIKE): "), vMsg);
                break;
            case AS_LOG_LEVEL_INFO:
                vMsg = rb_str_plus(rb_str_new2("INFO  (AEROSPIKE): "), vMsg);
                break;
            case AS_LOG_LEVEL_DEBUG:
                vMsg = rb_str_plus(rb_str_new2("DEBUG (AEROSPIKE): "), vMsg);
                break;
            case AS_LOG_LEVEL_TRACE:
                vMsg = rb_str_plus(rb_str_new2("DEBUG (AEROSPIKE): "), vMsg);
                break;
            default:
                vMsg = rb_str_plus(rb_str_new2("UNKNOWN (AEROSPIKE): "), vMsg);
                break;
            }
            // TODO: add default behavior with write into file
            fprintf(stdout, "%s\n", StringValueCStr(vMsg));
            return vMsg;
        }
        break;
    case T_OBJECT:
        switch(level) {
        case AS_LOG_LEVEL_ERROR:
            vMethod = rb_intern("error");
            break;
        case AS_LOG_LEVEL_WARN:
            vMethod = rb_intern("warn");
            break;
        case AS_LOG_LEVEL_INFO:
            vMethod = rb_intern("info");
            break;
        case AS_LOG_LEVEL_DEBUG:
        case AS_LOG_LEVEL_TRACE:
            vMethod = rb_intern("debug");
            break;
        default:
            vMethod = rb_intern("unknown");
            break;
        }

        return rb_funcall(vInternalLogger, vMethod, 1, vMsg);
    }

    return Qnil;
}