1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00
This commit is contained in:
gnosygnu
2015-05-10 23:00:43 -04:00
parent 0b5aa9aefe
commit 6eec99a713
157 changed files with 2148 additions and 975 deletions

View File

@@ -111,6 +111,12 @@ public class Bry_ {
catch (Exception e) {throw Err_.err_(e, "unsupported encoding");}
}
public static byte[] Coalesce(byte[] orig, byte[] val_if_not_blank) {return Bry_.Len_eq_0(val_if_not_blank) ? orig : val_if_not_blank;}
public static byte Get_at_end_or_fail(byte[] bry) {
if (bry == null) throw Err_.new_("bry is null");
int bry_len = bry.length;
if (bry_len == 0) throw Err_.new_("bry has 0 len");
return bry[bry_len - 1];
}
public static int While_fwd(byte[] src, byte while_byte, int bgn, int end) {
for (int i = bgn; i < end; i++)
if (src[i] != while_byte) return i;
@@ -427,11 +433,14 @@ public class Bry_ {
}
return true;
}
public static boolean HasAtBgn(byte[] src, byte lkp, int src_bgn) {
return src_bgn < src.length ? src[src_bgn] == lkp : false;
}
public static boolean HasAtBgn(byte[] src, byte[] lkp) {return HasAtBgn(src, lkp, 0, src.length);}
public static boolean HasAtBgn(byte[] src, byte[] lkp, int src_bgn, int srcEnd) {
int lkpLen = lkp.length;
if (lkpLen + src_bgn > srcEnd) return false; // lkp is longer than src
for (int i = 0; i < lkpLen; i++) {
public static boolean HasAtBgn(byte[] src, byte[] lkp, int src_bgn, int src_end) {
int lkp_len = lkp.length;
if (lkp_len + src_bgn > src_end) return false; // lkp is longer than src
for (int i = 0; i < lkp_len; i++) {
if (lkp[i] != src[i + src_bgn]) return false;
}
return true;

View File

@@ -34,6 +34,10 @@ public class Bry_finder {
if (src[i] == lkp) return i;
return Bry_finder.Not_found;
}
public static int Move_fwd(byte[] src, byte lkp, int cur, int end) {
int rv = Find_fwd(src, lkp, cur, src.length);
return rv == Bry_finder.Not_found ? rv : rv + 1;
}
public static int Move_fwd(byte[] src, byte[] lkp, int cur, int end) {
int rv = Find_fwd(src, lkp, cur, src.length);
return rv == Bry_finder.Not_found ? rv : rv + lkp.length;
@@ -191,6 +195,18 @@ public class Bry_finder {
}
}
}
public static int Find_fwd_until_ws(byte[] src, int cur, int end) {
while (true) {
if (cur == end) return Bry_finder.Not_found;
switch (src[cur]) {
case Byte_ascii.Space: case Byte_ascii.Tab: case Byte_ascii.NewLine: case Byte_ascii.CarriageReturn:
return cur;
default:
++cur;
break;
}
}
}
public static int Find_fwd_while_space_or_tab(byte[] src, int cur, int end) {
while (true) {
if (cur == end) return cur;
@@ -222,11 +238,13 @@ public class Bry_finder {
public static int Find_fwd_while_ws(byte[] src, int cur, int end) {
while (true) {
if (cur == end) return cur;
switch (src[cur]) {
case Byte_ascii.NewLine: case Byte_ascii.CarriageReturn:
case Byte_ascii.Space: case Byte_ascii.Tab: ++cur; break;
default: return cur;
}
try {
switch (src[cur]) {
case Byte_ascii.NewLine: case Byte_ascii.CarriageReturn:
case Byte_ascii.Space: case Byte_ascii.Tab: ++cur; break;
default: return cur;
}
} catch (Exception e) {throw Err_.new_("idx is invalid; cur={0} src={1} err={2}", cur, src, Err_.Message_gplx(e));}
}
}
public static int Find_fwd_while_letter(byte[] src, int cur, int end) {

View File

@@ -34,7 +34,11 @@ public class EnmMgr {
for (int i = 0; i < ary.length; i++) {
String term = String_.Trim(ary[i]); // ex: key.ctrl + key.a
if (prefix != null) term = String_.Replace(term, prefix, "");
int cur = Int_.cast_(rawRegy.FetchOrFail(term));
int cur = -1;
if (String_.HasAtBgn(term, "#"))
cur = Int_.parse_(String_.Mid(term, 1));
else
cur = Int_.cast_(rawRegy.Fetch(term));
rv |= cur;
}
return rv;

View File

@@ -26,6 +26,7 @@ public interface OrderedHash extends HashAdp {
void SortBy(ComparerAble comparer);
void ResizeBounds(int i);
Object Xto_ary(Class<?> t);
Object Xto_ary_and_clear(Class<?> t);
String XtoStr_ui();
void MoveTo(int src, int trg);
void Lock();

View File

@@ -39,6 +39,11 @@ public class OrderedHash_base extends HashAdp_base implements OrderedHash, GfoIn
ordered.Clear();
}
public Object Xto_ary(Class<?> type) {return ordered.Xto_ary(type);}
public Object Xto_ary_and_clear(Class<?> t) {
Object rv = Xto_ary(t);
this.Clear();
return rv;
}
@gplx.Virtual public void Sort() {if (locked) Lock_fail(); ordered.Sort();} // NOTE: uses item's .compareTo
public void SortBy(ComparerAble comparer) {if (locked) Lock_fail(); ordered.SortBy(comparer);}
@Override public java.util.Iterator iterator() {return ordered.iterator();}

View File

@@ -401,8 +401,8 @@ public class IoEngine_system extends IoEngine_base {
prog_dlg = xfer_fmt.usr_dlg;
if (!Web_access_enabled) {
if (prog_dlg != null) {
if (session_fil == null) session_fil = prog_dlg.Log_wtr().Session_dir().GenSubFil("internet.txt");
prog_dlg.Log_wtr().Log_msg_to_url_fmt(session_fil, "download disabled: src='~{0}' trg='~{1}'", xrg.Src(), xrg.Trg().Raw());
if (session_fil == null) session_fil = prog_dlg.Log_wkr().Session_dir().GenSubFil("internet.txt");
prog_dlg.Log_wkr().Log_msg_to_url_fmt(session_fil, "download disabled: src='~{0}' trg='~{1}'", xrg.Src(), xrg.Trg().Raw());
}
return false;
}
@@ -435,8 +435,8 @@ public class IoEngine_system extends IoEngine_base {
}
if (prog_dlg != null) {
xfer_fmt.Term();
if (session_fil == null) session_fil = prog_dlg.Log_wtr().Session_dir().GenSubFil("internet.txt");
prog_dlg.Log_wtr().Log_msg_to_url_fmt(session_fil, "download pass: src='~{0}' trg='~{1}'", src_str, xrg.Trg().Raw());
if (session_fil == null) session_fil = prog_dlg.Log_wkr().Session_dir().GenSubFil("internet.txt");
prog_dlg.Log_wkr().Log_msg_to_url_fmt(session_fil, "download pass: src='~{0}' trg='~{1}'", src_str, xrg.Trg().Raw());
}
return true;
}
@@ -446,8 +446,8 @@ public class IoEngine_system extends IoEngine_base {
else if (ClassAdp_.Eq_typeSafe(exc, java.io.FileNotFoundException.class)) xrg.Rslt_(IoEngine_xrg_downloadFil.Rslt_fail_file_not_found);
else xrg.Rslt_(IoEngine_xrg_downloadFil.Rslt_fail_unknown);
if (prog_dlg != null && !xrg.Prog_cancel()) {
if (session_fil == null) session_fil = prog_dlg.Log_wtr().Session_dir().GenSubFil("internet.txt");
prog_dlg.Log_wtr().Log_msg_to_url_fmt(session_fil, "download fail: src='~{0}' trg='~{1}' error='~{2}'", src_str, xrg.Trg().Raw(), Err_.Message_lang(exc));
if (session_fil == null) session_fil = prog_dlg.Log_wkr().Session_dir().GenSubFil("internet.txt");
prog_dlg.Log_wkr().Log_msg_to_url_fmt(session_fil, "download fail: src='~{0}' trg='~{1}' error='~{2}'", src_str, xrg.Trg().Raw(), Err_.Message_lang(exc));
}
if (trg_stream != null) {
try {
@@ -552,7 +552,7 @@ class Io_stream_rdr_http implements Io_stream_rdr {
if (!IoEngine_system.Web_access_enabled) {
read_done = read_failed = true;
if (prog_dlg != null)
prog_dlg.Log_wtr().Log_msg_to_url_fmt(session_fil, "download disabled: src='~{0}' trg='~{1}'", xrg.Src(), xrg.Trg().Raw());
prog_dlg.Log_wkr().Log_msg_to_url_fmt(session_fil, "download disabled: src='~{0}' trg='~{1}'", xrg.Src(), xrg.Trg().Raw());
return this;
}
src_str = xrg.Src();
@@ -604,12 +604,12 @@ class Io_stream_rdr_http implements Io_stream_rdr {
if (prog_dlg != null) {
xfer_fmt.Term();
}
if (session_fil == null && prog_dlg != null) session_fil = prog_dlg.Log_wtr().Session_dir().GenSubFil("internet.txt");
if (session_fil == null && prog_dlg != null) session_fil = prog_dlg.Log_wkr().Session_dir().GenSubFil("internet.txt");
if (read_failed) {
}
else {
if (prog_dlg != null)
prog_dlg.Log_wtr().Log_msg_to_url_fmt(session_fil, "download pass: src='~{0}' trg='~{1}'", src_str, xrg.Trg().Raw());
prog_dlg.Log_wkr().Log_msg_to_url_fmt(session_fil, "download pass: src='~{0}' trg='~{1}'", src_str, xrg.Trg().Raw());
xrg.Rslt_(IoEngine_xrg_downloadFil.Rslt_pass);
}
xrg.Prog_running_(false);
@@ -632,8 +632,8 @@ class Io_stream_rdr_http implements Io_stream_rdr {
else if (ClassAdp_.Eq_typeSafe(exc, java.io.FileNotFoundException.class)) xrg.Rslt_(IoEngine_xrg_downloadFil.Rslt_fail_file_not_found);
else xrg.Rslt_(IoEngine_xrg_downloadFil.Rslt_fail_unknown);
if (prog_dlg != null && !xrg.Prog_cancel()) {
if (session_fil == null) session_fil = prog_dlg.Log_wtr().Session_dir().GenSubFil("internet.txt");
prog_dlg.Log_wtr().Log_msg_to_url_fmt(session_fil, "download fail: src='~{0}' trg='~{1}' error='~{2}'", src_str, xrg.Trg().Raw(), Err_.Message_lang(exc));
if (session_fil == null) session_fil = prog_dlg.Log_wkr().Session_dir().GenSubFil("internet.txt");
prog_dlg.Log_wkr().Log_msg_to_url_fmt(session_fil, "download fail: src='~{0}' trg='~{1}' error='~{2}'", src_str, xrg.Trg().Raw(), Err_.Message_lang(exc));
}
this.Rls();
}

View File

@@ -217,9 +217,14 @@ public class ProcessAdp implements GfoInvkAble, RlsAble {
else if (!exe_url.OwnerDir().EqNull()) // only set workingDir if ownerDir is not null; NOTE: workingDir necessary for AdvMame; probably not a bad thing to do
pb.directory(new File(exe_url.OwnerDir().Xto_api()));
} ProcessBuilder pb;
Process Process_start() {
protected Process Process_start() {
try {process = pb.start();}
catch (IOException e) {throw Err_.err_key_(e, "gplx.ProcessAdp", "thread start failed");}
catch (IOException e) {
java.util.List<String> command_list = pb.command();
String[] command_ary = new String[command_list.size()];
command_ary = command_list.toArray(command_ary);
throw Err_.err_(e, "process:start failed; args={0}", String_.Concat_with_str(" ", command_ary));
}
return process;
}
void Process_run_and_end() {
@@ -290,50 +295,54 @@ class Thread_ProcessAdp_async extends Thread {
}
}
class Thread_ProcessAdp_sync extends Thread {
public Thread_ProcessAdp_sync(ProcessAdp process_adp) {this.process_adp = process_adp;} ProcessAdp process_adp;
public boolean Done() {return done;} boolean done = false;
public Thread_ProcessAdp_sync(ProcessAdp process_adp) {this.process_adp = process_adp;} private final ProcessAdp process_adp;
public boolean Done() {return done;} private boolean done = false;
public void Cancel() {
process_adp.UnderProcess().destroy();
}
public synchronized void run() {
done = false;
Process process = process_adp.Process_start();
StreamGobbler input_gobbler = new StreamGobbler("input", process.getInputStream());
StreamGobbler error_gobbler = new StreamGobbler("error", process.getErrorStream());
input_gobbler.start();
error_gobbler.start();
try {process.waitFor();}
catch (InterruptedException e) {
this.Cancel();
String kill_rslt = process_adp.Kill();
process_adp.Process_post(kill_rslt);
done = false;
return;
}
while (input_gobbler.isAlive()) {
try {input_gobbler.join(50);}
catch (InterruptedException e) {throw Err_.err_key_(e, "gplx.ProcessAdp", "thread interrupted at input gobbler");}
}
while (error_gobbler.isAlive()) {
try {error_gobbler.join(50);}
catch (InterruptedException e) {throw Err_.err_key_(e, "gplx.ProcessAdp", "thread interrupted at error gobbler");}
}
String result = input_gobbler.Rslt() + "\n" + error_gobbler.Rslt();
process_adp.Process_post(result);
done = true;
try {
Process process = process_adp.Process_start();
StreamGobbler input_gobbler = new StreamGobbler("input", process.getInputStream());
StreamGobbler error_gobbler = new StreamGobbler("error", process.getErrorStream());
input_gobbler.start();
error_gobbler.start();
try {process.waitFor();}
catch (InterruptedException e) {
this.Cancel();
String kill_rslt = process_adp.Kill();
process_adp.Process_post(kill_rslt);
done = false;
return;
}
while (input_gobbler.isAlive()) {
try {input_gobbler.join(50);}
catch (InterruptedException e) {throw Err_.err_key_(e, "gplx.ProcessAdp", "thread interrupted at input gobbler");}
}
while (error_gobbler.isAlive()) {
try {error_gobbler.join(50);}
catch (InterruptedException e) {throw Err_.err_key_(e, "gplx.ProcessAdp", "thread interrupted at error gobbler");}
}
String result = input_gobbler.Rslt() + "\n" + error_gobbler.Rslt();
process_adp.Process_post(result);
} catch (Exception e) { // NOTE: warn; do not throw, else multiple errors if timidity not available; PAGE:fr.u:Pentatoniques_altérées/Gammes_avec_deux_notes_altérées DATE:2015-05-08
Gfo_usr_dlg_.I.Warn_many("", "", "process.sync failed; cmd=~{0} args=~{1}", process_adp.Exe_url().Raw(), process_adp.Args_str());
}
finally {done = true;}
}
}
class StreamGobbler extends Thread {
String name; InputStream stream;
private final String name; private final InputStream stream;
public StreamGobbler (String name, InputStream stream) {this.name = name; this.stream = stream;}
public String Rslt() {return rslt;} String rslt;
public String Rslt() {return rslt;} private String rslt;
public void run () {
try {
String_bldr sb = String_bldr_.new_();
InputStreamReader isr = new InputStreamReader(stream);
BufferedReader br = new BufferedReader(isr);
while (true) {
String s = br.readLine ();
String s = br.readLine();
if (s == null) break;
sb.Add(s);
}

View File

@@ -16,21 +16,20 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
public interface Gfo_usr_dlg extends GfoInvkAble, Cancelable {
void Canceled_y_(); void Canceled_n_();
void Clear();
Gfo_usr_dlg_ui Ui_wkr(); void Ui_wkr_(Gfo_usr_dlg_ui v);
Gfo_log_wtr Log_wtr(); void Log_wtr_(Gfo_log_wtr v);
String Log_many(String grp_key, String msg_key, String fmt, Object... args);
String Warn_many(String grp_key, String msg_key, String fmt, Object... args);
Err Fail_many(String grp_key, String msg_key, String fmt, Object... args);
String Prog_many(String grp_key, String msg_key, String fmt, Object... args);
String Prog_none(String grp_key, String msg_key, String fmt);
String Note_many(String grp_key, String msg_key, String fmt, Object... args);
String Note_none(String grp_key, String msg_key, String fmt);
String Note_gui_none(String grp_key, String msg_key, String fmt);
String Prog_one(String grp_key, String msg_key, String fmt, Object arg);
String Prog_direct(String msg);
String Log_direct(String msg);
String Plog_many(String grp_key, String msg_key, String fmt, Object... args);
public interface Gfo_usr_dlg extends Cancelable {
void Canceled_y_(); void Canceled_n_();
Gfo_usr_dlg__log Log_wkr(); void Log_wkr_(Gfo_usr_dlg__log v);
Gfo_usr_dlg__gui Gui_wkr(); void Gui_wkr_(Gfo_usr_dlg__gui v);
String Log_many(String grp_key, String msg_key, String fmt, Object... args);
String Warn_many(String grp_key, String msg_key, String fmt, Object... args);
Err Fail_many(String grp_key, String msg_key, String fmt, Object... args);
String Prog_many(String grp_key, String msg_key, String fmt, Object... args);
String Prog_none(String grp_key, String msg_key, String fmt);
String Note_many(String grp_key, String msg_key, String fmt, Object... args);
String Note_none(String grp_key, String msg_key, String fmt);
String Note_gui_none(String grp_key, String msg_key, String fmt);
String Prog_one(String grp_key, String msg_key, String fmt, Object arg);
String Prog_direct(String msg);
String Log_direct(String msg);
String Plog_many(String grp_key, String msg_key, String fmt, Object... args);
}

View File

@@ -17,15 +17,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
public class Gfo_usr_dlg_ {
public static Gfo_usr_dlg I = Gfo_usr_dlg_null._;
public static final Gfo_usr_dlg Null = Gfo_usr_dlg_null._;
public static Gfo_usr_dlg I = Gfo_usr_dlg_noop._; // NOTE: global instance which can be reassigned
public static final Gfo_usr_dlg Noop = Gfo_usr_dlg_noop._;
public static Gfo_usr_dlg Test() {
if (test == null)
test = new Gfo_usr_dlg_base(Gfo_usr_dlg__log_.Noop, Gfo_usr_dlg__gui_.Test);
return test;
} private static Gfo_usr_dlg_base test;
}
class Gfo_usr_dlg_null implements Gfo_usr_dlg {
class Gfo_usr_dlg_noop implements Gfo_usr_dlg {
public boolean Canceled() {return false;} public void Canceled_y_() {} public void Canceled_n_() {}
public void Cancel() {} public void Cancel_reset() {}
public void Clear() {}
public Gfo_usr_dlg_ui Ui_wkr() {throw Err_.not_implemented_();} public void Ui_wkr_(Gfo_usr_dlg_ui v) {}
public Gfo_log_wtr Log_wtr() {throw Err_.not_implemented_();} public void Log_wtr_(Gfo_log_wtr v) {}
public Gfo_usr_dlg__log Log_wkr() {return Gfo_usr_dlg__log_.Noop;} public void Log_wkr_(Gfo_usr_dlg__log v) {}
public Gfo_usr_dlg__gui Gui_wkr() {return Gfo_usr_dlg__gui_.Noop;} public void Gui_wkr_(Gfo_usr_dlg__gui v) {}
public String Log_many(String grp_key, String msg_key, String fmt, Object... args) {return "";}
public String Warn_many(String grp_key, String msg_key, String fmt, Object... args) {return "";}
public Err Fail_many(String grp_key, String msg_key, String fmt, Object... args) {return Err_.new_(fmt);}
@@ -39,5 +44,5 @@ class Gfo_usr_dlg_null implements Gfo_usr_dlg {
public String Log_direct(String msg) {return "";}
public String Plog_many(String grp_key, String msg_key, String fmt, Object... args) {return "";}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {return this;}
public static final Gfo_usr_dlg_null _ = new Gfo_usr_dlg_null(); Gfo_usr_dlg_null() {}
public static final Gfo_usr_dlg_noop _ = new Gfo_usr_dlg_noop(); Gfo_usr_dlg_noop() {}
}

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
import gplx.core.strings.*;
public interface Gfo_usr_dlg_ui {
public interface Gfo_usr_dlg__gui {
void Clear();
String_ring Prog_msgs();
void Write_prog(String text);

View File

@@ -17,12 +17,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
import gplx.core.strings.*;
public class Gfo_usr_dlg_ui_ {
public static final Gfo_usr_dlg_ui Null = new Gfo_usr_dlg_ui_null();
public static final Gfo_usr_dlg_ui Console = new Gfo_usr_dlg_ui_console();
public static final Gfo_usr_dlg_ui Test = new Gfo_usr_dlg_ui_test();
public class Gfo_usr_dlg__gui_ {
public static final Gfo_usr_dlg__gui Noop = new Gfo_usr_dlg__gui_noop();
public static final Gfo_usr_dlg__gui Console = new Gfo_usr_dlg__gui_console();
public static final Gfo_usr_dlg__gui Test = new Gfo_usr_dlg__gui_test();
}
class Gfo_usr_dlg_ui_null implements Gfo_usr_dlg_ui {
class Gfo_usr_dlg__gui_noop implements Gfo_usr_dlg__gui {
public void Clear() {}
public String_ring Prog_msgs() {return ring;} String_ring ring = new String_ring().Max_(0);
public void Write_prog(String text) {}
@@ -30,7 +30,7 @@ class Gfo_usr_dlg_ui_null implements Gfo_usr_dlg_ui {
public void Write_warn(String text) {}
public void Write_stop(String text) {}
}
class Gfo_usr_dlg_ui_console implements Gfo_usr_dlg_ui {
class Gfo_usr_dlg__gui_console implements Gfo_usr_dlg__gui {
public void Clear() {}
public String_ring Prog_msgs() {return ring;} String_ring ring = new String_ring().Max_(0);
public void Write_prog(String text) {console.WriteTempText(text);}

View File

@@ -17,7 +17,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
import gplx.core.strings.*;
public class Gfo_usr_dlg_ui_test implements Gfo_usr_dlg_ui {
public class Gfo_usr_dlg__gui_test implements Gfo_usr_dlg__gui {
public String[] Xto_str_ary() {return msgs.XtoStrAry();}
public ListAdp Warns() {return warns;}
public String_ring Prog_msgs() {return ring;} String_ring ring = new String_ring().Max_(0);

View File

@@ -16,17 +16,16 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
public interface Gfo_log_wtr extends GfoInvkAble {
Io_url Session_dir();
Io_url Log_dir(); void Log_dir_(Io_url v);
Io_url Session_fil();
public interface Gfo_usr_dlg__log extends GfoInvkAble {
boolean Enabled(); void Enabled_(boolean v);
boolean Queue_enabled(); void Queue_enabled_(boolean v);
Io_url Log_dir(); void Log_dir_(Io_url v);
Io_url Session_dir();
Io_url Session_fil();
void Log_msg_to_url_fmt(Io_url url, String fmt, Object... args);
void Log_msg_to_session(String txt);
void Log_msg_to_session_fmt(String fmt, Object... args);
void Log_msg_to_session_direct(String txt);
void Log_err(String txt);
void Init();
void Term();
void Log_to_session(String txt);
void Log_to_session_fmt(String fmt, Object... args);
void Log_to_session_direct(String txt);
void Log_to_err(String txt);
void Log_term();
}

View File

@@ -16,21 +16,20 @@ You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
public class Gfo_log_wtr_ {
public static final Gfo_log_wtr Null = new Gfo_log_wtr_null();
public class Gfo_usr_dlg__log_ {
public static final Gfo_usr_dlg__log Noop = new Gfo_usr_dlg__log_noop();
}
class Gfo_log_wtr_null implements Gfo_log_wtr {
class Gfo_usr_dlg__log_noop implements Gfo_usr_dlg__log {
public Io_url Session_fil() {return Io_url_.Null;}
public Io_url Session_dir() {return Io_url_.Null;}
public Io_url Log_dir() {return Io_url_.Null;} public void Log_dir_(Io_url v) {}
public boolean Enabled() {return enabled;} public void Enabled_(boolean v) {enabled = v;} private boolean enabled;
public boolean Queue_enabled() {return queue_enabled;} public void Queue_enabled_(boolean v) {queue_enabled = v;} private boolean queue_enabled;
public void Log_msg_to_url_fmt(Io_url url, String fmt, Object... args) {}
public void Log_msg_to_session_fmt(String fmt, Object... args) {}
public void Log_msg_to_session(String txt) {}
public void Log_msg_to_session_direct(String txt) {}
public void Log_err(String txt) {}
public void Init() {}
public void Term() {}
public void Log_to_session_fmt(String fmt, Object... args) {}
public void Log_to_session(String txt) {}
public void Log_to_session_direct(String txt) {}
public void Log_to_err(String txt) {}
public void Log_term() {}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {return this;}
}

View File

@@ -0,0 +1,123 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
import gplx.core.strings.*;
public class Gfo_usr_dlg__log_base implements Gfo_usr_dlg__log {
private int archive_dirs_max = 8;
private Io_url log_dir, err_fil;
private OrderedHash queued_list = OrderedHash_.new_();
private Bry_fmtr fmtr = Bry_fmtr.tmp_(); private Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
public boolean Queue_enabled() {return queue_enabled;} public void Queue_enabled_(boolean v) {queue_enabled = v; if (!v) this.Flush();} private boolean queue_enabled;
public boolean Enabled() {return enabled;} public void Enabled_(boolean v) {enabled = v;} private boolean enabled = true;
public Io_url Session_dir() {return session_dir;} private Io_url session_dir;
public Io_url Session_fil() {return session_fil;} private Io_url session_fil;
private void Flush() {
int queued_len = queued_list.Count();
for (int i = 0; i < queued_len; i++) {
Usr_log_fil fil = (Usr_log_fil)queued_list.FetchAt(i);
if (fil.Url() == null) {
fil.Url_(session_dir.GenSubFil("session.txt"));
}
fil.Flush();
}
}
public Io_url Log_dir() {return log_dir;}
public void Log_dir_(Io_url log_dir) {
this.log_dir = log_dir;
session_dir = log_dir.GenSubDir(Dir_name_current);
session_fil = session_dir.GenSubFil("session.txt");
err_fil = session_dir.GenSubFil("err.txt");
}
public void Log_term() {
if (!enabled) return;
Io_url[] archive_dirs = Io_mgr._.QueryDir_args(log_dir).DirInclude_().DirOnly_().ExecAsUrlAry();
int archive_dirs_len = archive_dirs.length;
int session_cutoff = archive_dirs_len - archive_dirs_max;
for (int i = 0; i < session_cutoff; i++) {
Io_url archive_dir = archive_dirs[i];
Io_mgr._.DeleteDirDeep(archive_dir);
this.Log_to_session("archive dir del: " + session_dir.Raw());
}
this.Log_to_session("app term");
MoveCurrentToArchive(session_dir);
}
private void MoveCurrentToArchive(Io_url dir) {Io_mgr._.MoveDirDeep(dir, dir.OwnerDir().GenSubDir(DateAdp_.Now().XtoStr_fmt_yyyyMMdd_HHmmss_fff()));}
public void Log_info(boolean warn, String s) {if (warn) Log_to_err(s); else Log_to_session(s);}
public void Log_msg_to_url_fmt(Io_url url, String fmt, Object... args) {
if (!enabled) return;
String msg = Bld_msg(String_.new_utf8_(fmtr.Fmt_(fmt).Bld_bry_many(tmp_bfr, args)));
Log_msg(url, msg);
Log_msg(session_fil, msg);
}
public void Log_to_session_fmt(String fmt, Object... args) {Log_to_session(String_.new_utf8_(fmtr.Fmt_(fmt).Bld_bry_many(tmp_bfr, args)));}
public void Log_to_session(String s) {
if (!enabled) return;
String line = Bld_msg(s);
Log_msg(session_fil, line);
}
public void Log_to_session_direct(String s) {
if (!enabled) return;
Log_msg(session_fil, s);
}
public void Log_to_err(String s) {
if (!enabled) return;
try {
String line = Bld_msg(s);
Log_msg(session_fil, line);
Log_msg(err_fil, line);
}
catch (Exception e) {Err_.Noop(e);} // java.lang.StringBuilder can throw exceptions in some situations when called on a different thread; ignore errors
} private String_bldr sb = String_bldr_.new_thread(); // NOTE: use java.lang.StringBuffer to try to avoid random exceptions when called on a different thread
private String Bld_msg(String s) {return sb.Add(DateAdp_.Now().XtoUtc().XtoStr_fmt_yyyyMMdd_HHmmss_fff()).Add(" ").Add(s).Add_char_nl().Xto_str_and_clear();}
private void Log_msg(Io_url url, String txt) {
if (queue_enabled) {
String url_raw = url == null ? "mem" : url.Raw();
Usr_log_fil fil = (Usr_log_fil)queued_list.Fetch(url_raw);
if (fil == null) {
fil = new Usr_log_fil(url);
queued_list.Add(url_raw, fil);
}
fil.Add(txt);
}
else
Io_mgr._.AppendFilStr(url, txt);
}
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
if (ctx.Match(k, Invk_enabled_)) enabled = m.ReadYn("v");
else if (ctx.Match(k, Invk_archive_dirs_max_)) archive_dirs_max = m.ReadInt("v");
else if (ctx.Match(k, Invk_log_dir_)) log_dir = m.ReadIoUrl("v");
else return GfoInvkAble_.Rv_unhandled;
return this;
} public static final String Invk_enabled_ = "enabled_", Invk_archive_dirs_max_ = "archive_dirs_max_", Invk_log_dir_ = "log_dir_";
static final String Dir_name_log = "log", Dir_name_current = "current";
public static final Gfo_usr_dlg__log_base _ = new Gfo_usr_dlg__log_base();
}
class Usr_log_fil {
public Usr_log_fil(Io_url url) {this.url = url;}
public Io_url Url() {return url;} public Usr_log_fil Url_(Io_url v) {url = v; return this;} Io_url url;
public void Add(String text) {sb.Add(text);} String_bldr sb = String_bldr_.new_();
public void Flush() {
if (sb.Count() == 0) return;
try {
Io_mgr._.AppendFilStr(url, sb.Xto_str_and_clear());
}
catch (Exception e) {
ConsoleAdp._.WriteLine(Err_.Message_gplx_brief(e));
}
}
}

View File

@@ -0,0 +1,55 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012 gnosygnu@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx;
public class Gfo_usr_dlg_base implements Gfo_usr_dlg {
private final Bry_bfr tmp_bfr = Bry_bfr.reset_(255);
private final Bry_fmtr tmp_fmtr = Bry_fmtr.tmp_().Fail_when_invalid_escapes_(false); // do not fail b/c msgs may contain excerpt of random text; EX:[[User:A|~A~]] DATE:2014-11-28
public Gfo_usr_dlg_base(Gfo_usr_dlg__log log_wkr, Gfo_usr_dlg__gui gui_wkr) {this.log_wkr = log_wkr; this.gui_wkr = gui_wkr;}
public Gfo_usr_dlg__log Log_wkr() {return log_wkr;} public void Log_wkr_(Gfo_usr_dlg__log v) {log_wkr = v;} private Gfo_usr_dlg__log log_wkr;
public Gfo_usr_dlg__gui Gui_wkr() {return gui_wkr;} public void Gui_wkr_(Gfo_usr_dlg__gui v) {gui_wkr = v;} private Gfo_usr_dlg__gui gui_wkr;
public boolean Canceled() {return canceled;} public void Canceled_y_() {canceled = true;} public void Canceled_n_() {canceled = false;} private boolean canceled;
public void Cancel() {canceled = true;} public void Cancel_reset() {canceled = false;}
public String Log_many(String grp_key, String msg_key, String fmt, Object... args) {String rv = Bld_msg_many(grp_key, msg_key, fmt, args ); log_wkr.Log_to_session(rv); return rv;}
public String Warn_many(String grp_key, String msg_key, String fmt, Object... args) {String rv = Bld_msg_many(grp_key, msg_key, fmt, args ); log_wkr.Log_to_err(rv); gui_wkr.Write_warn(rv); return rv;}
public String Prog_many(String grp_key, String msg_key, String fmt, Object... args) {String rv = Bld_msg_many(grp_key, msg_key, fmt, args ); gui_wkr.Write_prog(rv); return rv;}
public String Prog_one(String grp_key, String msg_key, String fmt, Object arg) {String rv = Bld_msg_one (grp_key, msg_key, fmt, arg ); gui_wkr.Write_prog(rv); return rv;}
public String Prog_none(String grp_key, String msg_key, String fmt) {String rv = Bld_msg_none(grp_key, msg_key, fmt ); gui_wkr.Write_prog(rv); return rv;}
public String Prog_direct(String msg) { gui_wkr.Write_prog(msg); return msg;}
public String Log_direct(String msg) { log_wkr.Log_to_session(msg); return msg;}
public String Note_many(String grp_key, String msg_key, String fmt, Object... args) {String rv = Bld_msg_many(grp_key, msg_key, fmt, args ); log_wkr.Log_to_session(rv); gui_wkr.Write_note(rv); return rv;}
public String Note_none(String grp_key, String msg_key, String fmt) {String rv = Bld_msg_none(grp_key, msg_key, fmt ); log_wkr.Log_to_session(rv); gui_wkr.Write_note(rv); return rv;}
public String Note_gui_none(String grp_key, String msg_key, String fmt) {String rv = Bld_msg_none(grp_key, msg_key, fmt ); gui_wkr.Write_note(rv); return rv;}
public String Plog_many(String grp_key, String msg_key, String fmt, Object... args) {
String rv = Log_many(grp_key, msg_key, fmt, args);
return Prog_direct(rv);
}
public Err Fail_many(String grp_key, String msg_key, String fmt, Object... args) {
Err rv = Err_.new_(Bld_msg_many(grp_key, msg_key, fmt, args));
log_wkr.Log_to_err(Err_.Message_gplx(rv));
return rv;
}
private String Bld_msg_many(String grp_key, String msg_key, String fmt, Object[] args) {
tmp_fmtr.Fmt_(fmt).Bld_bfr_many(tmp_bfr, args);
return tmp_bfr.Xto_str_and_clear();
}
private String Bld_msg_one(String grp_key, String msg_key, String fmt, Object val) {
tmp_fmtr.Fmt_(fmt).Bld_bfr_one(tmp_bfr, val);
return tmp_bfr.Xto_str_and_clear();
}
private String Bld_msg_none(String grp_key, String msg_key, String fmt) {return fmt;}
}