1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef LENS_LOGGER_H
  27 #define LENS_LOGGER_H
  28 
  29 /**
  30  * Initialize the Glass logger
  31  *
  32  * Called from JNI_OnLoad
  33  */
  34 void glass_logger_init();
  35 
  36 /**
  37  * Log a message at the given logging level.
  38  * Not used directly. GLASS_LOG should be used instead.
  39  */
  40 void glass_logf(int level,
  41                 const char *func,
  42                 const char *file,
  43                 int line,
  44                 const char *format, ...);
  45 
  46 
  47 /**
  48  * Write a C and Java backtrace to stderr
  49  */
  50 void glass_backtrace();
  51 
  52 /**
  53  * The logging level.
  54  * Not used directly. GLASS_LOG and GLASS_IF_LOG should be used instead.
  55  */
  56 extern jint glass_log_level;
  57 
  58 /**
  59  * Begins a conditional statement that is only run if the current logging level
  60  * is less than or equal to "level".
  61  * For example, GLASS_IF_LOG(LOG_WARNING) { f(); } will call f() if and only if
  62  * the current logging settings include printing warning messages.
  63  * @param level The logging level to be tested against.
  64  */
  65 #define GLASS_IF_LOG(level) if (level >= glass_log_level)
  66 
  67 /**
  68  * Logs a message at the given logging level
  69  * @param level the logging level (e.g. LOG_WARNING)
  70  * @param ... a format string and parameters in printf format
  71  */
  72 /** Logging levels, with same meanings as in java.util.logging.Level */
  73 #define GLASS_LOG_LEVEL_SEVERE  1000
  74 #define GLASS_LOG_LEVEL_WARNING 900
  75 #define GLASS_LOG_LEVEL_INFO    800
  76 #define GLASS_LOG_LEVEL_CONFIG  700
  77 #define GLASS_LOG_LEVEL_FINE    500
  78 #define GLASS_LOG_LEVEL_FINER   400
  79 #define GLASS_LOG_LEVEL_FINEST  300
  80 
  81 #ifdef ANDROID_NDK
  82 // Can't use java logger in jvm8 on Android. Remove when this issue is fixed.
  83 #include <android/log.h>
  84 #define TAG "GLASS"
  85 #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, __VA_ARGS__))
  86 #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, __VA_ARGS__))
  87 #define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, __VA_ARGS__))
  88 #define GLASS_LOG(level,...) \
  89         GLASS_IF_LOG(level) \
  90         LOGI(TAG, __VA_ARGS__)
  91 
  92 #else
  93 #define GLASS_LOG(level,...) \
  94     GLASS_IF_LOG(level) \
  95     glass_logf(level, __func__, __FILE__, __LINE__, __VA_ARGS__)
  96 #endif
  97 
  98 #define GLASS_IF_LOG_SEVERE  GLASS_IF_LOG(GLASS_LOG_LEVEL_SEVERE)
  99 #define GLASS_IF_LOG_WARNING GLASS_IF_LOG(GLASS_LOG_LEVEL_WARNING)
 100 #define GLASS_IF_LOG_INFO    GLASS_IF_LOG(GLASS_LOG_LEVEL_INFO)
 101 #define GLASS_IF_LOG_CONFIG  GLASS_IF_LOG(GLASS_LOG_LEVEL_CONFIG)
 102 #define GLASS_IF_LOG_FINE    GLASS_IF_LOG(GLASS_LOG_LEVEL_FINE)
 103 #define GLASS_IF_LOG_FINER   GLASS_IF_LOG(GLASS_LOG_LEVEL_FINER)
 104 #define GLASS_IF_LOG_FINEST  GLASS_IF_LOG(GLASS_LOG_LEVEL_FINEST)
 105 
 106 #ifdef NO_LOGGING
 107 #define GLASS_LOG_SEVERE(...)  (void)0, ##__VA_ARGS__
 108 #define GLASS_LOG_WARNING(...) (void)0, ##__VA_ARGS__
 109 #define GLASS_LOG_INFO(...) (void)0, ##__VA_ARGS__
 110 #define GLASS_LOG_CONFIG(...) (void)0, ##__VA_ARGS__
 111 #define GLASS_LOG_FINE(...) (void)0, ##__VA_ARGS__
 112 #define GLASS_LOG_FINER(...) (void)0, ##__VA_ARGS__
 113 #define GLASS_LOG_FINEST(...) (void)0, ##__VA_ARGS__
 114 #else
 115 #define GLASS_LOG_SEVERE(...) GLASS_LOG(GLASS_LOG_LEVEL_SEVERE, __VA_ARGS__)
 116 #define GLASS_LOG_WARNING(...) GLASS_LOG(GLASS_LOG_LEVEL_WARNING, __VA_ARGS__)
 117 #define GLASS_LOG_INFO(...) GLASS_LOG(GLASS_LOG_LEVEL_INFO, __VA_ARGS__)
 118 #define GLASS_LOG_CONFIG(...) GLASS_LOG(GLASS_LOG_LEVEL_CONFIG, __VA_ARGS__)
 119 #define GLASS_LOG_FINE(...) GLASS_LOG(GLASS_LOG_LEVEL_FINE, __VA_ARGS__)
 120 #define GLASS_LOG_FINER(...) GLASS_LOG(GLASS_LOG_LEVEL_FINER, __VA_ARGS__)
 121 #define GLASS_LOG_FINEST(...) GLASS_LOG(GLASS_LOG_LEVEL_FINEST, __VA_ARGS__)
 122 #endif
 123 
 124 #endif // LENS_LOGGER_H