Android Open Source - client-android Q Log






From Project

Back to project page client-android.

License

The source code is released under:

Apache License

If you think the Android project client-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qmonix.sdk;
//from   w ww  .  ja  va  2s.co  m
import android.util.Log;


/**
 * Qmonix logging class that automates some logging tasks: it prints a log caller class,
 * method names, and line number, it stores and prints an application tag so user would not need to
 * tell it every time.
 *
 * Default logging level is INFO. You can change it with setLogLevel(). E.g.
 * QLog.setLogLevel(QLog.DEBUG_LEVEL);
 */
public class QLog {
  static public int DEBUG_LEVEL = 0;
  static public int INFO_LEVEL = 1;
  static public int WARNING_LEVEL = 2;
  static public int ERROR_LEVEL = 3;

  public static final String applicationTag = "QMONIX";

  static private boolean logClass = true;
  static private boolean logMethod = true;
  static private boolean logLineNr = true;

  static private int logLevel = INFO_LEVEL;

  static public int debug(String msg) {
    if (QLog.logLevel > DEBUG_LEVEL ) {
      return 0;
    }

    StringBuilder log = QLog.getCallerInfo(2);
    log.insert(0, "[DEBUG] ");
    log.append(msg);
    return Log.d(QLog.applicationTag, log.toString());
  }

  static public int info(String msg) {
    if (QLog.logLevel > INFO_LEVEL) {
      return 0;
    }

    StringBuilder log = QLog.getCallerInfo(2);
    log.insert(0, "[INFO] ");
    log.append(msg);
    return Log.i(QLog.applicationTag, log.toString());
  }

  static public int warning(String msg) {
    if (QLog.logLevel > WARNING_LEVEL) {
      return 0;
    }

    StringBuilder log = QLog.getCallerInfo(2);
    log.insert(0, "[WARNING] ");
    log.append(msg);
    return Log.w(QLog.applicationTag, log.toString());
  }

  /**
   * Sends error log message if verbose logging is ON. Adds additional info like caller
   * class and method names if QLog feature is turned ON (default).
   *
   * @param msg log message.
   * @return bytes written to output.
   */
  static public int error(String msg) {
    if (QLog.logLevel > ERROR_LEVEL) {
      return 0;
    }

    StringBuilder log = QLog.getCallerInfo(2);
    log.insert(0, "[ERROR] ");
    log.append(msg);
    return Log.e(QLog.applicationTag, log.toString());
  }

  /**
   * Sets minimum log level.
   *
   * @param level minimum log level.
   */
  static public void setLogLevel(int level) {
    QLog.logLevel = level;
  }


  /**
   * Turns on or off log caller class name printing together with log.
   *
   * @param log true if log function should print class name, otherwise false.
   */
  static public void setLogClass(boolean log) {
    QLog.logClass = log;
  }

  /**
   * Turns on or off log caller method name printing together with log.
   *
   * @param log true if log function should print method name, otherwise false.
   */
  static public void setLogMethod(boolean log) {
    QLog.logMethod = log;
  }

  /**
   * Turns on or off log caller line number printing together with log.
   *
   * @param log true if log function should print line number, otherwise false.
   */
  static public void setLogLineNr(boolean log) {
    QLog.logLineNr = log;
  }


  // Private methods.

  /**
   * Depending on set features returns a log caller information: caller class and/or method
   * name.
   *
   * @param level caller level in function call stack.
   * @return log caller information string.
   */
  static private StringBuilder getCallerInfo(int level) {
    StringBuilder result = new StringBuilder();

    try {
      throw new Exception();
    } catch (Exception e) {
      StackTraceElement ste = e.getStackTrace()[level];

      if (QLog.logClass) {
        result.append(ste.getClassName());
      }

      if (QLog.logMethod) {
        result.append(".");
        result.append(ste.getMethodName());
        result.append("()");
      }

      if (QLog.logLineNr) {
        result.append(":");
        result.append(ste.getLineNumber());
      }

      result.append(": ");
    }

    return result;
  }
}




Java Source Code List

com.qmonix.sample.basic.MainActivity.java
com.qmonix.sdk.EventDispatchHandler.java
com.qmonix.sdk.EventDispatcher.java
com.qmonix.sdk.EventMessage.java
com.qmonix.sdk.Event.java
com.qmonix.sdk.FireableTimingEvent.java
com.qmonix.sdk.HttpEventDispatcher.java
com.qmonix.sdk.LogEventDispatcher.java
com.qmonix.sdk.QLog.java
com.qmonix.sdk.TimingEvent.java
com.qmonix.sdk.Tracker.java
com.qmonix.sdk.VolumeEvent.java
com.qmonix.sdk.helpers.HttpHelper.java
com.qmonix.sdk.helpers.exceptions.HttpHelperException.java
com.qmonix.sdk.utils.AsyncTaskResult.java
com.qmonix.sdk.utils.Utils.java