OSDN Git Service

とりあえずコンパイルできるように。
[gokigen/ThetaThoughtShutter.git] / app / src / main / java / jp / osdn / gokigen / thetathoughtshutter / utils / SimpleLogDumper.kt
1 package jp.osdn.gokigen.thetathoughtshutter.utils
2
3 import android.app.Activity
4 import android.os.Environment
5 import android.util.Log
6 import jp.osdn.gokigen.thetathoughtshutter.R
7 import java.io.File
8 import java.io.FileOutputStream
9 import java.text.SimpleDateFormat
10 import java.util.*
11
12 object SimpleLogDumper
13 {
14     private val TAG = SimpleLogDumper::class.java.simpleName
15
16     /**
17      * デバッグ用:ログにバイト列を出力する
18      *
19      */
20     fun dumpBytes(header: String, data: ByteArray?)
21     {
22         if (data == null)
23         {
24             Log.v(TAG, "DATA IS NULL")
25             return
26         }
27         if (data.size > 8192)
28         {
29             Log.v(TAG, " --- DUMP DATA IS TOO LONG... " + data.size + " bytes.")
30             return
31         }
32         var index = 0
33         var message: StringBuffer
34         message = StringBuffer()
35         for (item in data)
36         {
37             index++
38             message.append(String.format("%02x ", item))
39             if (index >= 16)
40             {
41                 Log.v(TAG, "$header $message")
42                 index = 0
43                 message = StringBuffer()
44             }
45         }
46         if (index != 0)
47         {
48             Log.v(TAG, "$header $message")
49         }
50         System.gc()
51     }
52
53     fun binaryOutputToFile(activity: Activity, fileNamePrefix: String, rx_body: ByteArray)
54     {
55         try
56         {
57             val calendar = Calendar.getInstance()
58             val extendName = SimpleDateFormat("yyyyMMdd-HHmmss", Locale.getDefault()).format(calendar.time)
59             @Suppress("DEPRECATION") val directoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).path + "/" + activity.getString(R.string.app_name2) + "/"
60             val outputFileName = fileNamePrefix + "_" + extendName + ".bin"
61             val filepath = File(directoryPath.toLowerCase(Locale.ROOT), outputFileName.toLowerCase(Locale.ROOT)).path
62             val outputStream = FileOutputStream(filepath)
63             outputStream.write(rx_body, 0, rx_body.size)
64             outputStream.flush()
65             outputStream.close()
66         }
67         catch (e: Exception)
68         {
69             e.printStackTrace()
70         }
71     }
72 }