Search code examples
androidloggingdeviceprogram-entry-point

Unable to open log device '/dev/log/main': No such file or directory on my Nexus 4


As you can see my problem is : Unable to open log device '/dev/log/main': No such file or directory . The application i'm writing i'm testing on my nexus 4. How can i resolve the problem?


Solution

  • In Jelly Bean Google prevented arbitrary apps from accessing the system/app logs so unless your device is rooted you won't be able to access the log file, either.

    See http://commonsware.com/blog/2012/07/12/read-logs-regression.html.

    In order to allow on-device access of logs I created a Logger class that I use for all my debug logs. It basically wraps android.util.Log, outputting all the Logger.d calls to Log.d and to a file in the root directory. It doesn't yet output all the information contained in the Android logs, but you can edit it to put whatever info you'd like in there. Don't forget to go clear the file every once in a while... (: Here it is:

    package com.example.app;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import android.os.Environment;
    import android.util.Log;
    
    public class Logger
    {
    
        private static final String LOGTAG = "Logger";
    
        // Set to false to remove creation of local file
        private static final boolean IS_DEBUG_MODE = true;
    
        private static final SimpleDateFormat mSDF = new SimpleDateFormat( "MM-dd hh:mm:ss.SSS" );
    
        public Logger() {
    
        }
    
        public static void d( String tag, String msg ) {
            Log.d( tag, msg );
    
            writeToFile( tag, msg );
        }
    
        public static void d( String tag, String msg, Throwable th ) {
            Log.d( tag, msg, th );
    
            writeToFile( tag, msg, th );
        }
    
        private static void writeToFile( String tag, String msg ) {
    
            if( IS_DEBUG_MODE ) {
                File root = Environment.getExternalStorageDirectory();
                File file = new File( root, "appLog.txt" );
    
                try {
                    if( root.canWrite() ) {
                        FileWriter fileWriter = new FileWriter( file, true );
                        BufferedWriter out = new BufferedWriter( fileWriter );
    
                        Date d = new Date();
    
                        out.write( mSDF.format( d ) + ": " + tag + " : " + msg );
                        out.newLine();
                        out.close();
                    }
                } catch( IOException e ) {
                    Log.d( LOGTAG, "Couldn't write file: " + e.getMessage() );
                }
            }
        }
    
        private static void writeToFile( String tag, String msg, Throwable th ) {
            writeToFile( tag, msg + ", e: " + th.getMessage() );
        }
    
    }