1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package Torello.JavaDoc;

import Torello.JDUInternal.Messager.MsgControl;
import Torello.JDUInternal.MainJDU.ClassUpgradeData.UpgradeSettings;

import java.io.IOException;

import Torello.Java.FileRW;
import Torello.Java.UnreachableError;

class LogFile 
{
    static void set(
            final String                    logFileName,
            final UpgradeSettings.Builder   settingsBuilder,
            final String                    logFileHeader
        )
    {

        // This is just used / passed to the "Exception Checker" (below) to build a more
        // readable Exception-Message.

        final String fileDescription = "Disk / File-System Upgrader Log-Dump File";

        // Write log-file header.  Check that the log-file is accessible and writable.
        UpgradeException.checkFileIsWriteable(logFileName, fileDescription, logFileHeader);

        // Build a java.util.function.Consumer<String> 
        // This consumer will function as the log-file write-mechanism.

        settingsBuilder.logFile = new Appendable()
        {
            // This method is never actually used by the log-writes in JD-Upgrader.  Realize that
            // writing to the log, and actually check-pointing the log to disk are not the same
            // thing.  This appendable is used for actually writing out the log contents to a
            // flat-file (or any user-provided output/storing mechanism that the user can think of)
            //
            // The user has the option of writing the log-contents to some other, user-specified,
            // appendable that does whatever it wants with the log-contents.
            //
            // But whatever it is! - Check-pointing the log to it's output is only done in the
            // class Messager - using the method: Messager.checkPointLog();
            //
            // FURTHERMORE: The method "Messager.checkPointLog()" is only invoked twice!  Once by
            //              the class "ExtraFilesProcessor" and once by "MainFilesProcessor"

            public Appendable append(char c) // AGAIN: Not used
            { throw new UnreachableError(); }

            public Appendable append(CharSequence s, int start, int end) // NOT USED
            { throw new UnreachableError(); }

            // Invoked only once: In Messager.checkPointLog()
            public Appendable append(CharSequence s) 
            {
                try
                    { FileRW.appendToFile(s, logFileName); }

                catch (IOException ioe)
                {
                    throw new UpgradeException
                        ("Cannot write to log-file: [" + logFileName + "]", ioe);
                }

                return this;
            }
        };

        MsgControl.setLogAppendable(settingsBuilder.logFile);
    }
}