Charon logs to a file in the App's data directory
authorTobias Brunner <tobias@strongswan.org>
Fri, 10 Aug 2012 07:06:49 +0000 (09:06 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 13 Aug 2012 09:22:20 +0000 (11:22 +0200)
src/frontends/android/jni/libandroidbridge/charonservice.c
src/frontends/android/src/org/strongswan/android/logic/CharonVpnService.java

index 802e087..fab99ac 100644 (file)
@@ -19,6 +19,7 @@
 #include <string.h>
 #include <sys/utsname.h>
 #include <android/log.h>
+#include <errno.h>
 
 #include "charonservice.h"
 #include "android_jni.h"
@@ -117,6 +118,35 @@ static void dbg_android(debug_t group, level_t level, char *fmt, ...)
        }
 }
 
+/**
+ * Initialize file logger
+ */
+static void initialize_logger(char *logfile)
+{
+       file_logger_t *file_logger;
+       debug_t group;
+       FILE *file;
+
+       /* truncate an existing file */
+       file = fopen(logfile, "w");
+       if (!file)
+       {
+               DBG1(DBG_DMN, "opening file %s for logging failed: %s",
+                        logfile, strerror(errno));
+               return;
+       }
+       /* flush each line */
+       setlinebuf(file);
+
+       file_logger = file_logger_create(file, "%b %e %T", FALSE);
+       for (group = 0; group < DBG_MAX; group++)
+       {
+               file_logger->set_level(file_logger, group, ANDROID_DEBUG_LEVEL);
+       }
+       charon->file_loggers->insert_last(charon->file_loggers, file_logger);
+       charon->bus->add_logger(charon->bus, &file_logger->logger);
+}
+
 METHOD(charonservice_t, update_status, bool,
        private_charonservice_t *this, android_vpn_state_t code)
 {
@@ -353,10 +383,11 @@ static void segv_handler(int signal)
  * Initialize charon and the libraries via JNI
  */
 JNI_METHOD(CharonVpnService, initializeCharon, void,
-       jobject builder)
+       jobject builder, jstring jlogfile)
 {
        struct sigaction action;
        struct utsname utsname;
+       char *logfile;
 
        /* logging for library during initialization, as we have no bus yet */
        dbg = dbg_android;
@@ -392,6 +423,10 @@ JNI_METHOD(CharonVpnService, initializeCharon, void,
                return;
        }
 
+       logfile = androidjni_convert_jstring(env, jlogfile);
+       initialize_logger(logfile);
+       free(logfile);
+
        charonservice_init(env, this, builder);
 
        if (uname(&utsname) != 0)
index 069f000..c9c1ad0 100644 (file)
@@ -17,6 +17,7 @@
 
 package org.strongswan.android.logic;
 
+import java.io.File;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.SocketException;
@@ -46,6 +47,9 @@ import android.util.Log;
 public class CharonVpnService extends VpnService implements Runnable
 {
        private static final String TAG = CharonVpnService.class.getSimpleName();
+       public static final String LOG_FILE = "charon.log";
+
+       private String mLogFile;
        private VpnProfileDataSource mDataSource;
        private Thread mConnectionHandler;
        private VpnProfile mCurrentProfile;
@@ -113,6 +117,8 @@ public class CharonVpnService extends VpnService implements Runnable
        @Override
        public void onCreate()
        {
+               mLogFile = getFilesDir().getAbsolutePath() + File.separator + LOG_FILE;
+
                mDataSource = new VpnProfileDataSource(this);
                mDataSource.open();
                /* use a separate thread as main thread for charon */
@@ -204,7 +210,7 @@ public class CharonVpnService extends VpnService implements Runnable
                                                mIsDisconnecting = false;
 
                                                BuilderAdapter builder = new BuilderAdapter(mCurrentProfile.getName());
-                                               initializeCharon(builder);
+                                               initializeCharon(builder, mLogFile);
                                                Log.i(TAG, "charon started");
 
                                                String local_address = getLocalIPv4Address();
@@ -418,8 +424,9 @@ public class CharonVpnService extends VpnService implements Runnable
         * Initialization of charon, provided by libandroidbridge.so
         *
         * @param builder BuilderAdapter for this connection
+        * @param logfile absolute path to the logfile
         */
-       public native void initializeCharon(BuilderAdapter builder);
+       public native void initializeCharon(BuilderAdapter builder, String logfile);
 
        /**
         * Deinitialize charon, provided by libandroidbridge.so