b7f7e64c09668deef0088720cf5714709070fe00
[nit.git] / lib / android / log.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Advanced Android logging services
18 module log is ldflags "-llog"
19
20 import platform
21
22 in "C" `{
23 #include <android/log.h>
24 `}
25
26 # Use Android logs to print
27 redef fun print(object)
28 do
29 log_write(priority_info, app.log_prefix.to_cstring, object.to_s.to_cstring)
30 end
31
32 # Use Android logs to print errors
33 redef fun print_error(object)
34 do
35 log_write(priority_error, app.log_prefix.to_cstring, object.to_s.to_cstring)
36 end
37
38 # Use Android logs to print warnings
39 redef fun print_warning(object)
40 do
41 log_write(priority_warn, app.log_prefix.to_cstring, object.to_s.to_cstring)
42 end
43
44 redef class App
45 # Prefix to all log messages
46 protected fun log_prefix: String do return "app.nit"
47 end
48
49 # Default Android log priority
50 private fun priority_default: Int do return 1
51
52 # Verbose Android log priority
53 private fun priority_verbose: Int do return 2
54
55 # Debug Android log priority
56 private fun priority_debug: Int do return 3
57
58 # Info Android log priority
59 private fun priority_info: Int do return 4
60
61 # Warn Android log priority
62 private fun priority_warn: Int do return 5
63
64 # Error Android log priority
65 private fun priority_error: Int do return 6
66
67 # Fatal Android log priority
68 private fun priority_fatal: Int do return 7
69
70 # Silent Android log priority
71 private fun priority_silent: Int do return 8
72
73 # Write `text` to Android log at priority `level` with tag `tag`
74 private fun log_write(level: Int, tag, text: NativeString) `{
75 __android_log_write(level, tag, text);
76 `}