How can I use the iOS OSLog in Xamarin.iOS?
I did succeed in using NSLog as follows, but I see no way of setting the subsystem (to the bundle identifier) with NSLog so that I can use that to filter the logs in Console.app.
public class Logger
{
#if DEBUG
[DllImport(ObjCRuntime.Constants.FoundationLibrary)]
private extern static void NSLog(IntPtr message);
#endif
public void WriteLine(string line)
{
#if DEBUG
using (var nss = new NSString(line))
{
NSLog(nss.Handle);
}
#endif
}
}
In Xamarin.iOS, there is a static CoreFoundation.OSLog object called Default that one can use straight away which will log messages with the specified OSLogLevel argument such as OSLogLevel.Debug, OSLogLevel.Error etc.
You could have a method in your Xamarin.iOS code that logs a message at the Debug level:
using CoreFoundation;
// ...
public void Write(string message)
{
OSLog.Default.Log(OSLogLevel.Debug, message);
}
If you want to use the subsystem and category, you have to instantiate an instance of OSLog using the appropriate constructor and use that instance to log your messages. Presumably you'd want to hold a static reference to your created instance and use it like you'd use OSLog.Default.
public partial class AppDelegate : Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public static OSLog LoggerInstance;
public override bool FinishedLaunching(UIApplication app, NSDictionary launchOptions)
{
LoggerInstance = new OSLog(subsystem: "subsystem", category: "category");
//...
public class SomeClass
{
public void SomeMethod()
{
AppDelegate.LoggerInstance?.Log(OSLogLevel.Debug, "log message");
// ...
}
This will print a fairly nice message in the device log with a timestamp and showing the specified category.
OSLog is a ObjC struct (of two const chars) and a kernel method, os_log_create, is provided to allocate one.
Note: Refer to the os/log.h for details.
Define:
[DllImport("__Internal", EntryPoint = "os_log_create")]
public static extern IntPtr os_log_create(string subsystem, string category);
Usage:
var oslog = os_log_create("some.bundle.id", "StackOverflowCategory");
FYI: your NSLog should include a printf format string as a NSString
[DllImport (Constants.FoundationLibrary, EntryPoint = "NSLog")]
extern static void NSLog (IntPtr format, [MarshalAs (UnmanagedType.LPStr)] string s);
Related
I am building a data transfer app using direct Wifi. I am getting the string value of extension from Uri in the onActivityResult method and storing it in Shared preferences.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String extension;
Uri uri = data.getData();
Context context = getActivity().getApplicationContext();
extension = "."+getMimeType(context, uri);
SharedPreferences sharedPreferences = myContext.getSharedPreferences(myName, MODE_PRIVATE);
SharedPreferences.Editor myEdit = sharedPreferences.edit();
myEdit.putString("hello", extension);
myEdit.apply();
}
It is working fine till now as the value of extension is storing in shared preferences successfully. Now, in the onConnectionInfoAvailable method (related to the broadcast receiver), I am calling an AsyncTask.
#Override
public void onConnectionInfoAvailable(final WifiP2pInfo info) {
new FileServerAsyncTask().execute();
}
Now, I want to get the shared preference value in doInBackground method of AsyncTask. But I am not getting the value of extension from shared preferences, although my code is working fine. Please help.
public static class FileServerAsyncTask extends AsyncTask<Void, Void, String> {
#Override
protected String doInBackground(Void... params) {
try {
SharedPreferences sh = myContext.getSharedPreferences(myName, MODE_PRIVATE);
String extension = sh.getString("hello", "no");
}....
Note: myContext and myName are defined as public static variables.
I have one List box and I would like to set code type of it.
I create new AbstractCodeType :
public class MyCodeType extends AbstractCodeType<String, String> {
private static final long serialVersionUID = 6808664924551155395L;
public static final String ID = null;
#Override
public String getId() {
return ID;
}
#Order(10.0)
public static class UnknownCode extends AbstractCode<String> {
private static final long serialVersionUID = -1307260056726644943L;
public static final String ID = "Unknown";
#Override
protected String getConfiguredText() {
return TEXTS.get("Unknown");
}
#Override
public String getId() {
return ID;
}
}
}
and I set this code type in list box :
#Override
protected Class<? extends ICodeType<?, String>> getConfiguredCodeType() {
return MyCodeType.class;
}
But doesn't work. It return empty box.
While I was debugging I noticed that in AbstractListBox.class in initConfig method it call this code type and set code type in m_lookupCall inside setCodeTypeClass. Then inside execLoadTableData, it get call but this call return empty array when called call.getDataByAll().
I suspect that converting between code type and Lookup call does not work properly.
EDIT
I try to debug where is the problem and if follow the path :
initConfig() -> CodeLookupCall.newInstanceByService(m_codeTypeClass); (line 581)
and if you look inside CodeLookupCall ;
getDataByAll() in line 221 `resolveCodes(v)` -> BEANS.opt(m_codeTypeClass) -> bean.getInstance() -> m_producer.produce(this) -> return (T) getCache().get(createCacheKey(type));
This is in class CodeService.class in line 97 :
Class<T> type is right class and createCacheKey(type) return not null object but then getCache().get(...) return null. From this point on everything is null (what is reasonable regarding that getCodeType return null.)
This is what I found out while debugging, if it helps someone to figure out what is wrong.
It looks like your codetype class is not found by the bean manager. CodeService only finds CodeTypes in its classpath (accessible in the server).
-> You might need to move your class to the shared project.
You can find examples for code types in the contacts demo application:
https://github.com/BSI-Business-Systems-Integration-AG/org.eclipse.scout.docs/tree/releases/5.2.x/code/contacts
I tested your code snippet with Eclipse Scout Neon M4 and I could reproduce your described error.
However, it seems that this bug has been fixed with Scout Neon M5. So I suggest that you upgrade to the latest milestone version, which is recommended anyway.
This is a follow-up question to How to write void pointer typedefs in vapi files?
I now have four almost identical [Compact] classes that represent handles allocated with unixODBCs SQLAllocHandle function.
The first one (for the ENV type handle) looks like this:
[CCode (cname = "void", free_function = "EnvironmentHandle.free")]
[Compact]
public class EnvironmentHandle {
[CCode (cname = "SQLAllocHandle")]
private static Return allocate_internal (HandleType type, void* nothing, out EnvironmentHandle output_handle);
public static Return allocate (out EnvironmentHandle output_handle) {
return allocate_internal (HandleType.ENV, null, out output_handle);
}
[CCode (cname = "SQLFreeHandle")]
private static Return free_internal (HandleType type, EnvironmentHandle handle);
public static Return free (EnvironmentHandle handle) {
return free_internal (HandleType.ENV, handle);
}
}
This doesn't compile.
Is it possible to use a static class method as the free_function?
If not, is there at least a way to write a custom free_function in the vapi file?
I need a custom function because the SQLFreeHandle function takes the handle type and the handle as an argument.
From the vapi users perspective all that is really important is:
[CCode (cname = "void")]
[Compact]
public class EnvironmentHandle {
public static Return allocate (out EnvironmentHandle output_handle);
}
The only other solution would be to use a [SimpleType] struct as suggested by apmasell in the original question. That would hide the fact that a SQLHANLDE is really a reference type.
The full code of my current implementation is available online:
https://github.com/antiochus/unixodbc-vala/tree/0486f54dc3f86d9c8bf31071980e4f171aca9591
No. The free_function is a C function, not a Vala function and it cannot take any context. You have two options:
Write a C macro in an extra header file to do what you want and bind the macro as the free function.
Bind the free function as a static method that takes an owned instance of the object:
[CCode (cname = "SQLFreeHandle")]
public static Return free(HandleType type, owned EnvironmentHandle handle);
EnvrionmentHandle foo = ...;
EnvironmentHandle.free(HandleType.ENV, (owned) foo);
I am studying orchard architecture.i have faced with a strange concept in display management section.
in Partial view page there is a 'function call like' syntax like so Display(Model.Head). that is not a function thought, it is a dynamic object defined in orchard WebViewPage.
I am wondering how to define a dynamic object so that you can call it (and pass it an argument as well) like a function as i mentioned.
thanks in advance.
A lighter weight way to do it without clay would be to subclass the built-in DynamicObject class.
public static dynamic Display;
void Main()
{
Display = new MyCallableObject();
//this is what i was after
Console.Write(Display("bla bla bla"));
}
public class MyCallableObject:DynamicObject
{
public override bool TryInvoke(InvokeBinder binder, object[] args, out Object result)
{
result = string.Format("This is response for {0}",args.FirstOrDefault());
return true;
}
}
I finally found it my self!
all the operations have done with Clay Library behind the scene.i have wrote a sample console app for demonstrating the process.
class Program
{
static void Main(string[] args)
{
Display = ClayActivator.CreateInstance<MyResponser>(new List<IClayBehavior> {new MyFunctionCallBehavior()});
//this is what i was after
Console.Write(Display("bla bla bla"));
}
public static dynamic Display;
}
public class MyFunctionCallBehavior : IClayBehavior
{
public object InvokeMember(Func<object> proceed, object self, string name, INamedEnumerable<object> args)
{
return ((MyResponser)self).ResponseForRequest(args.FirstOrDefault().ToString());
}
}
public class MyResponser
{
public string ResponseForRequest(string param)
{
return string.Format("This is response for {0}",param);
}
}
Does anyone know of a MonoMac sample that shows how to implement Print (to a printer)? I haven't been able to find one.
I don't know of one, but the conceptual docs from Apple are relevant, and their sample snippets should be straightforward to port to C#: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Printing/Printing.html
I created a PrintDocument class a bit like this:
(You'd need to set a proper size and add some drawing in drawrect)
public class PrintDocument:NSView {
NSPrintOperation MyPrinter = null;
static IntPtr selCurrentContext = Selector.GetHandle ("currentContext");
static IntPtr classNSGraphicsContext = Class.GetHandle ("NSGraphicsContext");
public PrintDocument ()
{
MyPrinter=NSPrintOperation.FromView(this);
this.SetFrameSize(new SizeF(600,800));
}
public void Print ()
{
MyPrinter.RunOperation()
}
public override void DrawRect (RectangleF dirtyRect)
{
var context = new NSGraphicsContext (Messaging.IntPtr_objc_msgSend (classNSGraphicsContext, selCurrentContext));
//NSPrintOperation.CurrentOperation
}
}